Home > Database > Mysql Tutorial > How to Update a Table's Account Number Using Data from Another Table Based on Matching IDs?

How to Update a Table's Account Number Using Data from Another Table Based on Matching IDs?

Barbara Streisand
Release: 2025-01-23 00:45:11
Original
351 people have browsed it

How to Update a Table's Account Number Using Data from Another Table Based on Matching IDs?

Database table data synchronization: update account number based on ID matching

In database operations, you often need to synchronize data in different tables based on public IDs. The goal of this example is to update the table named Sales_Import so that its AccountNumber fields match the corresponding RetrieveAccountNumber in the AccountNumber table.

The

solution is to use a JOIN statement that contains a UPDATE clause. By joining these two tables via a common LeadID field, we establish a relationship between records. This allows us to access the required data in the RetrieveAccountNumber table and update the Sales_Import fields in the AccountNumber table.

MS SQL syntax

<code class="language-sql">UPDATE
    Sales_Import
SET
    Sales_Import.AccountNumber = RAN.AccountNumber
FROM
    Sales_Import SI
INNER JOIN
    RetrieveAccountNumber RAN
ON 
    SI.LeadID = RAN.LeadID;</code>
Copy after login

MySQL and MariaDB syntax

<code class="language-sql">UPDATE
    Sales_Import SI,
    RetrieveAccountNumber RAN
SET
    SI.AccountNumber = RAN.AccountNumber
WHERE
    SI.LeadID = RAN.LeadID;</code>
Copy after login

After executing this query, the Sales_Import fields in the AccountNumber table will be updated with the corresponding RetrieveAccountNumber values ​​in the AccountNumber table. This data synchronization ensures that you use account numbers consistently across your database.

The above is the detailed content of How to Update a Table's Account Number Using Data from Another Table Based on Matching IDs?. 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