Home > Database > Mysql Tutorial > How to Update a Table With Data from Another Table in MySQL?

How to Update a Table With Data from Another Table in MySQL?

Linda Hamilton
Release: 2024-10-30 03:46:02
Original
512 people have browsed it

How to Update a Table With Data from Another Table in MySQL?

Advanced MySql Query: Update Table with Information from Another Table

Updating a table with data from another table can be a valuable technique in database management. In this instance, we aim to update the "email" column in the "business" table with corresponding data from the "people" table.

The provided query aims to achieve this by matching "business_id" values in both tables and updating only records where the "email" column in the "business" table is empty. However, the query is missing a crucial component: the actual update statement. To correct this, the following query can be employed:

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 = ''
Copy after login

This enhanced query correctly sets the "email" column in the "business" table to the corresponding email from the "people" table, considering the specified conditions:

  • The "business_id" values in both tables must match.
  • Only the first person associated with a particular business ("sort_order" = '1') is considered.
  • The update only applies to records in the "business" table where the "email" column is currently empty.

The above is the detailed content of How to Update a Table With Data from Another Table in MySQL?. 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