开发者

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

开发者 https://www.devze.com 2023-01-04 05:05 出处:网络
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

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 开发者_Go百科giving me an error.

UPDATE table1
   SET field1 = table2.field1
  FROM Table1,Table2
 WHERE Table1.entry = Table2.entry


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消