Translate MySQL query to PostgreSQL - mysql

Having a problem with the plugin DMSF in redmine I have found a link with a SQL query that could fix the issue, however I am using PostgreSQL and that query returns a syntax error directly on f.project_id.
The MySQL query is:
update dmsf_files f
set f.project_id = (select d.project_id from dmsf_folders d where d.id = f.dmsf_folder_id and d.system = 1)
where (select dmsf_folders.system from dmsf_folders where dmsf_folders.id = f.dmsf_folder_id) = 1;
What should be the PostgreSQL equivalent ?
I have found some online Database syntax translator but unfortunately with no success at all.

Remove the alias from the left side of the SET clause:
update dmsf_files f
set project_id = (select d.project_id from dmsf_folders d
where d.id = f.dmsf_folder_id and d.system = 1)
where (select dmsf_folders.system from dmsf_folders
where dmsf_folders.id = f.dmsf_folder_id) = 1;

Related

UPDATE Query Giving me an SQL Error (1064)

I am trying to update a table in my database from another table. Here is my Syntax, but I can't seem to find any issues with it. I keep getting SQL Error (1064).
UPDATE customers b
SET customers.takerid = customer_update_2016.ot
FROM customer_update_2016 a, customers b
WHERE a.phone = b.phone && a.lname = b.lname
SQL Error (1064): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'FROM customer_update_2016 a, customers b WHERE a.phone = b.phone & a.lname =b' at line 3
Solution:
UPDATE customers
INNER JOIN customer_update_2016
ON customers.phone = customer_update_2016.phone
AND customers.lname = customer_update_2016.lname
SET customers.takerid = customer_update_2016.ot
you have both mysql and sql-server
which one?
UPDATE customers
SET customers.takerid = customer_update_2016.ot
FROM customers
JOIN customer_update_2016
on customers.phone = customer_update_2016.phone
and customers.lname = customer_update_2016.lname
and customers.takerid <> customer_update_2016.ot
Follow something like this always
UPDATE [table1_name] AS t1
INNER JOIN [table2_name] AS t2
ON t1.[column1_name] = t2.[column1_name]
SET t1.[column2_name] = t2.[column2_name];
To solve your problem you should use JOIN, in my case I had to pass data from one table to another like you and this was the way that solved my problem, hope it help with yours and others.(I'm using MariaDB v10.4)
UPDATE
table1
LEFT JOIN table2 AS tb2
ON tb2.fieldThatJoin = table1.fieldThatJoin//this part indicate the field that join them
SET
table1.field1 = tb2.field1;
//if you want to update more than one field then do this
table1.field1 = tb2.field1,
table1.field2 = tb2.field2,
table1.field3 = tb2.field3;//remember to put the ';' at the end

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.

Update table in MS Access from views exported from SQL

I have created a table and need to update the entries by comparing the data from the view that I have created in SQL server
This action need to performed by a button click in MS Access forms
I have written the following query in MS Access - Query. Exported the view totaltemp1 from sql server.
update P_Payroll as p
inner join totaltemp1 as tp on p.emp_id = tp.emp_id
inner join P_Student_Supervisor as ss on tp.emp_id = ss.emp_id
set Number_of_Hours = tp.temp_drop_hours,
Total_Payment = tp.temp_drop_hours*ss.emp_hourlywage
where p.emp_id = tp.emp_id and ss.emp_id = p.emp_id;
Previously I got this working correctly in SQL server in stand alone mode. Code for that as follows.
update P_Payroll set Number_of_Hours = tp.temp_drop_hours, Total_Payment = tp.temp_drop_hours*ss.emp_hourlywage
from totaltemp1 as tp, P_Payroll as p, P_Student_Supervisor as ss
where p.emp_id = tp.emp_id and ss.emp_id = p.emp_id;
I have checked the data structure too. I am just getting an error
"Syntax error (missing operator) in query expression 'p.emp_id = tp.emp_id inner join P_Student_Supervisor as ss on tp.emp_id = ss.emp_i"
Thanks for your time
Pk.
Two things you can check... first, remove the WHERE clause - you're already specifying those conditions in the JOIN statements.
Then, put brackets around your joins...
UPDATE (P_Payroll AS p
INNER JOIN totaltemp1 AS tp on p.emp_id = tp.emp_id)
INNER JOIN P_Student_Supervisor AS ss on tp.emp_id = ss.emp_id
SET p.Number_of_Hours = tp.temp_drop_hours,
p.Total_Payment = tp.temp_drop_hours * ss.emp_hourlywage
Access like brackets...

MySQL update query does not work #1064

Can anyone tell me what is wrong with this Query ?
UPDATE
violetta.navi__stop
SET
violetta.navi__stop.endHour = VB.endHour
FROM
violetta.navi__stop AS V
INNER JOIN
violetta_back.navi__stop AS VB
ON V.code = VB.code
This code sample works for me on my database:
SELECT
V.code, V.endHour
FROM
violetta.navi__stop AS V
UNIQUE
violetta_back.navi__stop AS VB
ON V.code = VB.code
Try this instead:
UPDATE
violetta.navi__stop v
INNER JOIN
violetta_back.navi__stop AS VB
ON V.code = VB.code
SET
v.endHour = VB.endHour

hibernate, mysql

i have the following hql query:
UPDATE TaskAssessment taskAssessment
SET taskAssessment.activeFlag = false
WHERE taskAssessment IN
(
SELECT taskAssessment2
FROM TaskAssessment taskAssessment2
Where taskAssessment2.activeFlag = true
AND taskAssessment2.patient.id
AND taskAssessment2.needsLevel.careNeed = :careNeed
)
but its giving me errors:
You can't specify target table 'TASK_ASSESSMENT' for update in FROM clause
could anyone help me to correct the query for mysql and hibernate. thanks in advance.
To resolve You can't specify target table 'TASK_ASSESSMENT' for update in FROM clause, rewrite the query to use JOIN instead of IN (in mysql you need to write something like this):
UPDATE TaskAssessment a
INNER JOIN TaskAssessment a2 ON (a2.id = a.id)
SET a.activeFlag = 0
WHERE a2.active_flag = 1 AND
a2.patient_id = :patient_id AND a2.needsLevel_careNeed = :careNeed