Why is this query giving me a syntax error? - mysql

Everytime I try to compile this code on DBeaver, it gives me a syntax error right where the first AND statement is located. I don't know how to solve it.
SELECT idintervento as _IDInterventoRif, idcdc, idcartella,
(SELECT COUNT(DISTINCT idintervento) as nInt
FROM cch.pats_cch_interventi i
WHERE (idtiporecord in('UTI','INT'))
AND i.idcdc = inter.idcdc
AND i.idcartella = inter.idcartella
AND i.utiingressodata < inter.utiingressodata
AND i.idintervento <> inter.idintervento
) as _IDInterventoTipoN
FROM cch.pats_cch_interventi inter
WHERE (idtiporecord in('INT') OR idtiporecord IS NULL)
AND idintervento <> ?
AND idcdc = ?
AND idcartella = ?
AND (dataintervento < ?
OR dataintervento = ? AND idintervento < ?)
ORDER BY idcdc, idcartella, utiingressodata desc;

Related

How to get records that match value or exist in another table?

I am trying to figure out how to get all tasks in this case that two of the fields equal a certain value or they exist in the other table?
Here is the query:
SELECT TASKS.task_id, TASKS.task_title, TASKS.task_description, TASKS.task_assigned_name, TASKS.task_assigned_phone_number, TASKS.task_due_date_time, TASKS.task_category
FROM TASKS
WHERE TASKS.task_complete = 1 AND
(TASKS.task_creator_id = ? OR
TASKS.task_assigned_user_id = ? OR
WHERE EXISTS (SELECT WATCHERS.task_id
FROM WATCHERS
WHERE WATCHERS.task_id = TASK.task_id AND
WATCHERS.watcher_user_id = ?
)
);
This is not returning anything even though I am expecting a result from my db.
You seem to have an error in your syntax. You have too many WHEREs:
SELECT t.task_id, t.task_title, t.task_description, t.task_assigned_name, t.task_assigned_phone_number, t.task_due_date_time, t.task_category
FROM TASKS t
WHERE t.task_complete = 1 AND
(t.task_creator_id = ? OR
t.task_assigned_user_id = ? OR
EXISTS (SELECT 1 -- the return value is immaterial
FROM WATCHERS w
WHERE w.task_id = t.task_id AND
w.watcher_user_id = ?
)
);
The WHERE before EXISTS is not appropriate.
Your query should be returning an error. Be sure to check for errors!
Have you tried using a join?
SELECT TASKS.task_id,
TASKS.task_title,
TASKS.task_description,
TASKS.task_assigned_name,
TASKS.task_assigned_phone_number,
TASKS.task_due_date_time,
TASKS.task_category
FROM TASKS
JOIN WATCHERS on WATCHERS.task_id = TASK.task_id
WHERE TASKS.task_complete = 1 AND
(TASKS.task_creator_id = ? OR
TASKS.task_assigned_user_id = ? OR
WATCHERS.watcher_user_id = ?);
I'm not sure if that's the logic you are looking for.
besides the extra where in your query, looks like you may have an extra closed parenthesis.
This sql matches the result set you posted in your duplicate post that has sample data and result:
SELECT t.task_id, t.task_complete, t.task_creator_id, t.task_assigned_user_Id
FROM tasks t
WHERE t.task_complete = 1 AND
(
t.task_creator_id = 8
OR t.task_assigned_user_id = 8
OR EXISTS
(
SELECT w.task_id
FROM watchers w
WHERE w.task_id = t.task_id
AND w.watcher_user_id = 8
)
)

use sql SELECT nodejs

I tried to use the select sql option in node js sql plugins.
But I get a error with this syntax :
con.query( 'SELECT token FROM utilisateursinvitation WHERE nomwork = ? AND
Rejoint = ? EXCEPT SELECT token FROM utilisateursinvitation WHERE nomwork
= ? AND Rejoint = ? AND nomdugroupe = ? ',
[nomwork,tatazueyzyu,"nomdugroupe"], function (error, results, fields) {
and the error :
Error: ER_PARSE_ERROR: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'EXCEPT SELECT token FROM utilisateursinvitation WHERE nomwork = 'nomdugroupe' ' at line 1
Because I don't know the correct syntax.
Can you help me ? THanks!
EXCEPT is not MySQL syntax.
Instead, you can use NOT IN or NOT EXISTS:
SELECT ui.token
FROM ui.utilisateursinvitation ui
WHERE ui.nomwork = ? AND ui.Rejoint = ? AND
NOT EXISTS (SELECT
FROM utilisateursinvitation ui2
WHERE ui2.token = ui.token AND
ui2.nomwork = ? AND ui2.Rejoint = ? AND
nomdugroupe = ?
);
Or, if you are trying to fine tokens that have nomwork and Rejoint as the input values, but not a particular nomdugroupe:
SELECT ui.token
FROM ui.utilisateursinvitation ui
WHERE ui.nomwork = ? AND ui.Rejoint = ?
GROUP BY ui.token
HAVING SUM(nomdugroupe = ?) > 0;

SQL - Update table

I need to update a table and this is my sql code so far but i'm getting the following error message:
Line 30: Incorrect syntax near ')'.
UPDATE dbo.Part
SET SupplierShortName = NationalSupplier.ShortName,
SupplierLongName = NationalSupplier.LongName
SELECT *
FROM dbo.Part
JOIN dbo.NationalSupplier
ON Part.SupplierNumber = NationalSupplier.Number
AND (ISNULL(Part.SupplierShortName,'') <> ISNULL(NationalSupplier.ShortName,'')
OR ISNULL(Part.SupplierLongName,'') <> ISNULL(NationalSupplier.LongName,''))
LEFT OUTER JOIN dbo.NationalPart
ON Part.NationalPartID = NationalPart.NationalPartID
WHERE Part.DWCreationEntityID = 1
AND NationalPart.NationalPartID is NULL
Thanks in advance for any valuable tips !
Try this:
UPDATE dbo.Part
SET SupplierShortName = NationalSupplier.ShortName,
SupplierLongName = NationalSupplier.LongName
FROM dbo.Part
JOIN dbo.NationalSupplier
ON Part.SupplierNumber = NationalSupplier.Number
AND (ISNULL(Part.SupplierShortName,'') <> ISNULL(NationalSupplier.ShortName,'')
OR ISNULL(Part.SupplierLongName,'') <> ISNULL(NationalSupplier.LongName,''))
LEFT OUTER JOIN dbo.NationalPart
ON Part.NationalPartID = NationalPart.NationalPartID
WHERE Part.DWCreationEntityID = 1
AND NationalPart.NationalPartID is NULL
You should alias the table names to make things concise.

Can I have a standalone clause in ActiveRecord?

I want to factor out the where clause in my two queries like this:
where_clause = where("(created_at >= ? and created_at < ?) or (updated_at >= ? and updated_at < ?)", range_start_date, range_end_date, range_start_date, range_end_date)
#book_instances = BookInstance.limit(pagination.items_per_page).
offset((pagination.page - 1) * pagination.items_per_page).
where_clause.
order(:id)
#pagination.total_items = BookInstance.where_clause.count
But ActiveRecord is not happy. I thought you could break out the query bits independently.
I ended up doing this, though I'm still wondering if I'm missing another nicer way:
book_instances_in_date_range = BookInstance.where(
"(created_at >= ? and created_at < ?) or (updated_at >= ? and updated_at < ?)",
range_start_date, range_end_date, range_start_date, range_end_date)
#pagination.total_items = book_instances_in_date_range.count
#book_instances = book_instances_in_date_range.limit(pagination.items_per_page).
offset((pagination.page - 1) * pagination.items_per_page).
order(:id)

correction for mysql query of rails

While executing the below mysql query in rails
cnt = Domainurl.find_by_sql ["SELECT Max(`count`)FROM `domainurls` WHERE `domaindetail_id`= ?",#domain.id]
urlcr=Domainurl.find_by_sql ["SELECT * FROM `domainurls` WHERE `domaindetail_id` = ? AND `count` = ?",cnt.count]
urlcr.each do |cr|
puts cr.url
end
I am getting error :
"error is: undefined method `closed?' for nil:NilClass"
the problem is at your last query
urlcr=Domainurl.find_by_sql ["SELECT * FROM `domainurls` WHERE `domaindetail_id` = ? AND `count` = ?",cnt.count]
you try to find something by 2 parameters ( domaindetail_id and count) but you provide only count. (you must provide domaindetail_id too)
urlcr=Domainurl.find_by_sql ["SELECT * FROM `domainurls` WHERE `domaindetail_id` = ? AND `count` = ?",#domain.id, cnt.count] #observe that I added #domain.id
You can rewrite that like
cnt = Domainurl.where(:domaindetail_id => #domain.id).maximum(:count) # this returns a number
urlcr=Domainurl.where("domaindetail_id = ? AND count = ?", #domain.id, cnt)