How to update fields from another table in MySQLPosted by Stanislav Furman on May 28, 2014
This article is to continue the series of short handy MySQL tips that I started a while ago.
If you need to update a number of column fields in a MySQL table with data from another table, there is a simple way to do it. Lets say you need to update user phone numbers in table1 with the corresponding phone numbers from table2. Then you could run the following query.
UPDATE table2 t2, table1 t1 SET t1.phone = t2.phone WHERE t2.user_id = t1.user_id
That's all! Now table1 will have updated data. Keep in mind that user ids must correspond to each other in both tables. And yeah, always have a back up before you do major changes in production database. ;)
Leave your comment