Home >Database >Mysql Tutorial >How to Update a Table with Data from Another Table Using SQL?

How to Update a Table with Data from Another Table Using SQL?

Patricia Arquette
Patricia ArquetteOriginal
2024-11-02 21:11:02965browse

How to Update a Table with Data from Another Table Using SQL?

Updating MySQL Table with Data from Another Table

In this scenario, you seek to update the email column in the "business" table with data from the corresponding rows in the "people" table. The tables are linked through the "business_id" column. To achieve this, we employ the following advanced SQL query:

UPDATE business b, people p
   SET b.email = p.email
 WHERE b.business_id = p.business_id
   AND p.sort_order = '1'
   AND b.email = ''

This query effectively updates the "business" table's email column with the email information from the "people" table for those rows where the "business_id" values match and the "sort_order" value for the "people" table is '1'. It also ensures that only rows with an empty string for the email column in the "business" table are updated.

By using the JOIN operation between the two tables, this query elegantly merges the data from both sources, allowing you to perform targeted updates based on specific conditions. This advanced query technique enables you to maintain data integrity and achieve desired updates seamlessly.

The above is the detailed content of How to Update a Table with Data from Another Table Using SQL?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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