Pages

Sunday, June 06, 2010

mysql update from another table

Recently I needed to update a bunch of rows in mysql with information from another table. I will explain how to do this simple process.



  1. Open up phpmyadmin or similar database editor

  2. view the sql query below



Code (sql)






  1. UPDATE updatefrom p, updateto pp



  2. SET pp.last_name = p.last_name



  3. WHERE pp.visid = p.id




Lets say we have 2 tables one named main and one named updateto and one named updatefrom, In the first line we assign variables to these t tables (p and pp) from there we can call column names using this format table.columname. If we had first_name and last_name in both tables and wanted to update the "updateto" table with the info from "updatefrom" this is how you would write it.


Source : http://worcesterwideweb.com/2007/07/03/mysql-update-from-another-table/

No comments: