It is easy to insert a row into a MySQL table from another MySQL table:
INSERT INTO sometable (SELECT * FROM anothertable WHERE somefield='somevalue')
but how do I do this if I want to update instead of insert? I’m looking for something similar to
UPDATE sometable SET (SELECT * FROM anothertable WHERE somefield='somevalue')
Obviously, this doesn’t work. (The structures of sometable and anothertable are identical).
If you’re trying to update rows based specifically on matches of the primary key, then you can use
REPLACEinstead of INSERT.This means when the primary key column from
anothertablehas the same value as the primary key column insometable, then all the other columns returned by the SELECT are used to overwrite those columns insometable.See http://dev.mysql.com/doc/refman/5.5/en/replace.html
If the row matching is based on something other than primary key, you’ll have to use multi-table UPDATE syntax as @John Woo showed.