Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL Query to Update a Field with data from another Field when two Fields Match

I need to update the content of one data field in a table with the content of another field in a table every time two separate fields, one on each table, match up. I've been trying this syntax but I just can't get it to work properly without giving me an error.

UPDATE table1
   SET field1 = table2.field1
  FROM Table1,Table2
 WHERE Table1.entry = Table2.entry
like image 990
Joe Avatar asked Dec 28 '25 16:12

Joe


1 Answers

update ... from is sql server's syntax. In MySQL you can just use multiple tables directly:

update
  table1 t1
  join table2 t2 on t2.field = t1.field
set
  t1.field1 = t2.matchingfield
where
  t1.whatever = t2.whatever

All is detailed on the MySQL update reference page.

like image 140
Donnie Avatar answered Dec 30 '25 07:12

Donnie



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!