I am updating a table based on a value in another table but I cannot figure out why it is throwing an error at the FROM clause:
UPDATE sn
SET sn.delay = 'Yes'
FROM arh ar
INNER JOIN tbl_salesactivity_new sn ON sn.shipment = ar.shipment_id
WHERE ar.ardelay > '0';
In MySQL the correct syntax to update a table using a JOIN operation is following:
UPDATE tbl_salesactivity_new sn
INNER JOIN arh ar
ON sn.shipment = ar.shipment_i
AND ar.ardelay > '0'
SET sn.delay = 'Yes'
To learn more about this syntax, check the examples found at this link.
EDIT. Adding another option >> moving the condition in the WHERE clause (ysth's suggestion)
UPDATE tbl_salesactivity_new sn
INNER JOIN arh ar
ON sn.shipment = ar.shipment_i
SET sn.delay = 'Yes'
WHERE ar.ardelay > '0'
I fired this query where I join two tables and output some of the columns of both tables:
SELECT B.option_id, B.product_id, A.title, B.identifier
FROM `catalog_product_option_title` A JOIN
`catalog_product_option` B
ON A.option_id = B.option_id
WHERE A.title = "Breite"
Result:
Now I need to enter the example value xyz on the column identifier in the result, everywhere. I would go ahead and do this by hand.
How can I make use of the update statement from MySQL to solve this without having to manually change it by hand?
I tried it like this:
UPDATE `catalog_product_option`
SET identifier = 'xyz'
WHERE option_id IN (
SELECT A.option_id
FROM `catalog_product_option_title` A
JOIN
`catalog_product_option` B
ON A.option_id = B.option_id
WHERE A.title = "Breite"
)
But the simulation of this query returned that this would change 0 lines.
UPDATE
I called the sql without simulating it, and now I get this error:
1093 - Table 'catalog_product_option' is specified twice, both as a target for 'UPDATE' and as a separate source for data
You could rewrite your query as a JOIN:
UPDATE `catalog_product_option` B
JOIN `catalog_product_option_title` A ON A.option_id = B.option_id
SET B.identifier = 'xyz' WHERE A.title = "Breite"
Can you try like this please?
UPDATE `catalog_product_option`
SET identifier = 'xyz'
WHERE option_id IN (
SELECT option_id FROM (SELECT A.option_id
FROM `catalog_product_option_title` A
JOIN
`catalog_product_option` B
ON A.option_id = B.option_id
WHERE A.title = "Breite") as x
)
Im trying to change some records in my database but sadly my sql knowledge is a bit limited. After googling and reading stuff online I have managed to write a select statement in which i can find the records that I want to update but i dont understand the logic to write the update statement to do it. I have to make several similar update statements so I hope this one I can figure out the rest myself
This is the select statement I have:
SELECT
MG.id,
MG.status,
MG.fin,
MG.execDateTime,
EXISTS
(
SELECT 1
FROM Mtask T
JOIN MTaskHis TH ON TH.t_id= T.id
WHERE T.tg_id = MG.id
AND YEAR(TH.dateTime) = 2019
) AS hasExecStart,
NMG.id,
NMG.execDateTime,
EXISTS
(
SELECT 1
FROM Mtask T
JOIN MTaskHis TH ON TH.t_id = T.id
WHERE T.tg_id = NMG.id
AND YEAR(TH.dateTime) = 2019
) AS hasExecNext
FROM Management_Group MG
JOIN MT_Groupman MTGM ON
MG.tgm_id = MTGM.id
LEFT JOIN Management_Group NMG ON MTGM.id =
NMG.tgm_id AND YEAR(NMG.execDateTime) = 2019
JOIN Management_Man MM ON MTGM.man_id = MM.id
JOIN Location L ON MM.location_id = L.id
WHERE L.org_id = 69
AND MG.stat != 'DELETED'
AND YEAR(MG.execDateTime) = 2018
AND MM.Type= 9
AND MG.fin != 1
AND EXISTS
(
SELECT 1
FROM Mtask T
WHERE T.tg_id = MG.id
AND T.stat = 'execution'
)
HAVING hasExecNext = 0 AND hasExecStart = 1
I know standard updates in sql:
UPDATE <TABLENAME>
SET <fieldName> = <value>
WHERE <conditons>
Except I do not know how to convert this select statement I have made into an update statement, reason for that is:
- Where do I put the exist alias in the update statement
- I also dont understand when or where to put all the JOINS in the from statement
- What about the HAVING
What is the best way to do joined updates like this?
In an UPDATE you can join the table you want to update to a sub-query that contains your current query.
UPDATE YourTable t
JOIN
(
<< add your query here >>
) q ON q.SomeKeyField = t.SomeKeyField
SET t.FieldName = q.FieldNameFromSubquery,
t.OtherFieldName = q.OtherFieldNameFromSubquery
I hope you can help me again, thanks already for pointing me to the right direction with creating the check digit for the new IBAN in Germany. I am now trying to update our membership database with the newly calculated BIC and IBAN but seem to have a problem with the UPDATE statement on the MariaDB MySQL database, despite the fact that I think I got the syntax right.
All I am trying to do is set the two fields "konto_bic" and "konto_iban" in the table "mitglieder" from the SELECT statement which creates a temporary table called b with the columns "id", "bic" and "iban". The "id" is the same in the two tables.
Here is my first try:
update a
set a.`konto_bic` = b.`BIC`, a.`konto_iban` = b.`IBAN`
from `mitglieder` a
INNER JOIN (SELECT m.`id`, m.`nachname`, m.`vorname`, m.`konto_bank`, m.`konto_blz`, m.`konto_nummer`, k.`bic` AS 'BIC', CONCAT('DE',LPAD(98-MOD(CONVERT(CONCAT(m.`konto_blz`,LPAD(m.`konto_nummer`,10,'0'),'1314','00'), decimal(24)),97),2,'0'),m.`konto_blz`,LPAD(m.`konto_nummer`,10,'0')) AS 'IBAN'
FROM `mitglieder` m
LEFT JOIN `konvert_bic_blz` k
ON m.`konto_blz` = k.`blz`
ORDER BY m.`nachname`, m.`vorname`) b
ON a.`id` = b.`id`
However, this produced an error and I tried this instead:
update `mitglieder` a
set a.`konto_bic` = b.`bic`, a.`konto_iban` = b.`iban`
FROM (SELECT m.`id` as 'id', k.`bic` as 'bic', CONCAT('DE',LPAD(98-MOD(CONVERT(CONCAT(m.`konto_blz`,LPAD(m.`konto_nummer`,10,'0'),'1314','00'), decimal(24)),97),2,'0'),m.`konto_blz`,LPAD(m.`konto_nummer`,10,'0')) AS 'iban'
FROM `mitglieder` m
LEFT JOIN `konvert_bic_blz` k
ON m.`konto_blz` = k.`blz`) b
WHERE a.`id` = b.`id`
That also did not get me any further (error from DB).
Can anyone see what my syntax error might be?
Thank you in advance for your help
Stephan
Try below SQL
UPDATE `mitglieder` a,
(SELECT m.`id` AS 'id',
k.`bic` AS 'bic',
CONCAT('DE',LPAD(98-MOD(CONVERT(CONCAT(m.`konto_blz`,LPAD(m.`konto_nummer`,10,'0'),'1314','00'), decimal(24)),97),2,'0'),m.`konto_blz`,LPAD(m.`konto_nummer`,10,'0')) AS 'iban'
FROM `mitglieder` m
LEFT JOIN `konvert_bic_blz` k ON m.`konto_blz` = k.`blz`) b
SET a.`konto_bic` = b.`bic`, a.`konto_iban` = b.`iban`
WHERE a.`id` = b.`id`
UPDATE Syntax
http://dev.mysql.com/doc/refman/5.0/en/update.html
I tried this on MariaDB 10.2.6 :
SET sql_mode = 'ANSI_QUOTES'
UPDATE "test"."user" AS "a"
INNER JOIN "test"."user_profile" AS "c" ON "c".user_id = "a".id
INNER JOIN "test"."profile" AS "d" ON "d".id = "c".profile_id
SET "a".firstname = 'laurent'
WHERE "a".id = 3;
And it works :)
I have a problem with updating records in my table. I am doing research all day but it is just beyond me.
Basics: I have two tables
TABLE1
TABLE2
I need to update nr_g from TABLE1 with id from TABLE2 but only where 'kraj' 'region' 'nazwa_hotelu' from TABLE1 is equal 'country' 'region' 'hotelName' from TABLE2
my trying so far:
UPDATE merlinx u
LEFT JOIN
merlinx_new s ON u.nr_g != s.id
SET
u.nr_g = s.id
WHERE
u.kraj = s.country AND u.nazwa_hotelu = s.hotelName AND u.region = s.region
That is updating me only 4 rows... and 1592 are unsafe statements
another shot of mine:
UPDATE merlinx_merged
SET
nr_g = (SELECT
merlinx_new.id
FROM
merlinx_new
INNER JOIN
merlinx_merged
WHERE
merlinx_new.country = merlinx_merged.kraj
AND merlinx_new.hotelName = merlinx_merged.nazwa_hotelu
AND merlinx_new.region = merlinx_merged.region)
And that is just throwing errors.
My mind is fried after 8 hours wasted on it. Help is much appreciated.
I think your issue is in your join statement. You have
LEFT JOIN merlinx_new s ON u.nr_g != s.id
You shouldn't have to have the ON criteria in that (if I'm understanding your question correctly).
This should do the trick if you want to overwrite merlinx.nr_g with the value from merlinx_new.id if all of your criteria matches in the WHERE clause.
UPDATE merlinx u, merlinx_new s
SET u.nr_g = s.id
WHERE u.kraj = s.country AND u.nazwa_hotelu = s.hotelName AND u.region = s.region