Home >Database >Mysql Tutorial >How to Update a MySQL Table Using Values from Another Table?
Updating MySQL Table from Another Table's Value
Given two tables with different structures, the task is to update the rows of one table (Table2) based on the values of another (Table1). Specifically, when Table2.SERIAL_ID matches Table1.SUBST_ID, it needs to retrieve and assign Table1.CREATED_ID to Table2.BRANCH_ID.
To accomplish this, an SQL UPDATE statement can be used with a JOIN clause to merge the data from both tables:
UPDATE TABLE2 JOIN TABLE1 ON TABLE2.SERIAL_ID = TABLE1.SUBST_ID SET TABLE2.BRANCH_ID = TABLE1.CREATED_ID;
By using this query, the BRANCH_ID column in Table2 will be updated to reflect the corresponding values in the CREATED_ID column of Table1, where the SERIAL_ID and SUBST_ID columns match. The result will be a table with the specified updates, aligning the data between the two tables.
The above is the detailed content of How to Update a MySQL Table Using Values from Another Table?. For more information, please follow other related articles on the PHP Chinese website!