Skip to content
Advertisement

MySQL table update after multi field check

Hi I have two table one like this one: table1 enter image description here

and one like this: table2

enter image description here

I would like to update all the fields on the table2 column “newID” based on this rules: if (table2.ID = table1.ID_actual or table2.ID=table1.ID_old) then table2.newID = table1.newID How can I resolve this problem ?

Advertisement

Answer

You need a join of the 2 tables in the UPDATE statement:

UPDATE table2 t2
INNER JOIN table1 t1 ON t2.ID IN (t1.ID_actual, t1.ID_old)
SET t2.newID = t1.newID
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement