HOWTO: MySQL - Update Data in one table from another table
Tuesday, November 18th, 2008
I have just been refactoring a legacy database and needed to update some data in one table from another table in MySQL.
In this case the 2 tables had the same values for the identity colomn.
Syntax:
UPDATE tablename1 t1 SET colomn1 = (SELECT colomn1 from tablename2 t2 WHERE t1.id = t2.id);
For example
If i had tables: ‘products’ and ‘merchandise’ and needed to copy the product title into the merchandise title colomn, I would simply:
UPDATE merchandise m SET title = (SELECT title from products p where m.id = p.id);
