Joe
Joe

Reputation: 33

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

Upvotes: 3

Views: 6569

Answers (1)

Donnie
Donnie

Reputation: 46923

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.

Upvotes: 7

Related Questions