I’m trying to update a table that is joined with another one to update the right record.
Here is my command so far:
UPDATE
links l
SET
l.l_id = `[value-1]`,
l.l_facebook = `[value-2]`,
l.l_youtube = `[value-3]`,
l.l_twitter = `[value-4]`,
l.l_googleplus = `[value-5]`,
l.l_rss = `[value-6]`,
l.l_homepage = `[value-7]`,
l.l_freigegeben = `[value-8]`
JOIN
sponsering ON l.l_id = sponsering.links_l_id
WHERE
sponsering.s_userID = 2
Trying to run the command in phpmyadmin gives me the following error message:
1064 - 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 'JOIN sponsering ON l.l_id = sponsering.links_l_id WHERE
sponsering.s_user' at line 12
I need to join the table sponsering because this gives me the correct record in the links table.
How can I solve this?
Try this
UPDATE
links l,
sponsering s
SET
l.l_faceook = `[value-2]`,
l.l_youtube = `[value-3]`,
l.l_twitter = `[value-4]`,
l.l_googleplus = `[value-5]`,
l.l_rss = `[value-6]`,
l.l_homepage = `[value-7]`,
l.l_freigegeben = `[value-8]`
WHERE
l.l_id = s.links_l_id AND
sponsering.s_userID = 2
In MySQL, the join is part of the update statement itself. There is no separate from:
UPDATE links l JOIN
sponsering s
ON l.l_id = s.links_l_id
SET
l.l_id = `[value-1]`,
l.l_faceook = `[value-2]`,
l.l_youtube = `[value-3]`,
l.l_twitter = `[value-4]`,
l.l_googleplus = `[value-5]`,
l.l_rss = `[value-6]`,
l.l_homepage = `[value-7]`,
l.l_freigegeben = `[value-8]`
WHERE s.s_userID = 2;
Some other databases use a FROM clause for the same purpose.
Related
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;
I have the following SQL statement:
UPDATE tbl_Invoices
SET tbl_Invoices.Base = tbl_Bases.BasePrice
FROM tbl_Invoices
INNER JOIN tbl_Bases ON tbl_Bases.ProductNumber = tbl_Invoices.ProductNumber
AND tbl_Bases.ChangeOrderID = tbl_Invoices.ChangeOrderID
AND tbl_Bases.CustomerName = 'VALEO'
AND tbl_Bases.CountryCode = 'FR'
AND tbl_Bases.ContractYear = 0
Access keep telling me I have a syntax error (missing operator)
This drives me nuts. Can someone tell me what's wrong?
UPDATE ... FROM doesn't exist in Access SQL, here you do it like this:
UPDATE tbl_Invoices
INNER JOIN tbl_Bases ON tbl_Bases.ProductNumber = tbl_Invoices.ProductNumber
AND tbl_Bases.ChangeOrderID = tbl_Invoices.ChangeOrderID
AND tbl_Bases.CustomerName = 'VALEO'
AND tbl_Bases.CountryCode = 'FR'
AND tbl_Bases.ContractYear = 0
SET tbl_Invoices.Base = tbl_Bases.BasePrice
Thanks a lot.
I still have an error as the = 'VALEO' etc... are not allowed in Join but this is working if moved to a where clause:
UPDATE tbl_Invoices
INNER JOIN tbl_Bases ON tbl_Bases.ProductNumber = tbl_Invoices.ProductNumber
AND tbl_Bases.ChangeOrderID = tbl_Invoices.ChangeOrderID
SET tbl_Invoices.Base = tbl_Bases.BasePrice
WHERE tbl_Bases.CustomerName = 'VALEO'
AND tbl_Bases.CountryCode = 'FR'
AND tbl_Bases.ContractYear = 0
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
Please let me know why I get an error on the following SQL (I am using a MySQL database) and how to rectify it:
SELECT at_award_description.ad_id,
at_award_description.ad_detail,
at_award_description.ad_archived_date,
a.cad_id,
a.cad_task_completion_date
FROM at_award_description
LEFT JOIN at_cub_award_date AS a ON ((at_award_description.ad_id = a.ad_id)
AND (a.ca_id = 37)
AND a.cad_task_completion_date = (SELECT MAX(b.cad_task_completion_date)
FROM at_cub_award_date AS b
WHERE a.ca_id = b.ca_id AND a.ad_id = b.ad_id
)
)
WHERE at_award_description.aw_id = 5
ORDER BY at_award_description.ad_order;
I broke the SQL down to ensure each part worked.
SELECT MAX(b.cad_task_completion_date)
FROM at_cub_award_date AS b
And
SELECT at_award_description.ad_id,
at_award_description.ad_detail,
at_award_description.ad_archived_date,
a.cad_id,
a.cad_task_completion_date
FROM at_award_description
LEFT JOIN at_cub_award_date AS a ON ((at_award_description.ad_id = a.ad_id)
AND (a.ca_id = 37)
)
WHERE at_award_description.aw_id = 5
ORDER BY at_award_description.ad_order;
Each of these work on their own. When I combine them I get the following error:
Error
SQL query:
LIMIT 0, 25
MySQL said:
#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 'LIMIT 0, 25' at line 1
Regards,
Glyn
EDIT
#Barmar came up with the solution to remove the ";" at the end when testing. However, the result was not what I expected so I ended up changing this to:
SELECT at_award_description.ad_id,
at_award_description.aw_id,
at_award_description.ad_group,
at_award_description.ad_detail,
at_award_description.ad_start_date,
at_award_description.ad_archived_date,
MAX(at_cub_award_date.cad_task_completion_date)
FROM at_award_description
LEFT JOIN at_cub_award_date ON ((at_award_description.ad_id = at_cub_award_date.ad_id)
AND (at_cub_award_date.ca_id = 37))
WHERE at_award_description.aw_id = 5
GROUP BY at_award_description.ad_group
ORDER BY at_award_description.ad_order, at_award_description.ad_group
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.