Home > Database > Mysql Tutorial > How to Update a MySQL Table Using Values from Another Table?

How to Update a MySQL Table Using Values from Another Table?

Mary-Kate Olsen
Release: 2024-12-16 00:24:12
Original
834 people have browsed it

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;
Copy after login

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template