6

I can do the following to insert records into a table from a select on another table:

INSERT INTO table (field1, field2) SELECT field1,field2 FROM table2

Can I do the same with an update ?? Something like this (not working!):

UPDATE table SET field1=table2.field1, field2=table2.field2 SELECT field1,field2 FROM table2
WHERE table.field0=table2.field0

I know how to do this with only 1 field, but is there a way to do it with multiple fields?

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
Dylan
  • 9,129
  • 20
  • 96
  • 153

2 Answers2

11
UPDATE table A INNER JOIN table2 B USING (field0)
SET A.field1 = B.field1,A.field2 = B.field2;
RolandoMySQLDBA
  • 43,883
  • 16
  • 91
  • 132
-1

Yes, like this:

UPDATE table SET 
field1=(select field1 from table2 WHERE field0=table1.field0)
field2=(select field2 from table2 WHERE field0=table1.field0)
-- WHERE some condition (optional)
Bohemian
  • 412,405
  • 93
  • 575
  • 722