Updating Account Numbers in SQL: A Matching ID Approach
This article demonstrates how to efficiently update account numbers in one SQL table based on matching IDs in another. We'll use an SQL UPDATE
query to achieve this.
The scenario involves two tables: Sales_Import
(containing account numbers) and RetrieveAccountNumber
(containing card numbers and the matching LeadID
). The goal is to update the AccountNumber
field in Sales_Import
with data from RetrieveAccountNumber
, matching on the LeadID
column.
A robust solution uses an UPDATE
statement with a JOIN
operation. This method directly updates the target table while referencing data from the second table, ensuring accurate updates.
MS SQL Server Syntax:
<code class="language-sql">UPDATE Sales_Import SET AccountNumber = RAN.AccountNumber FROM Sales_Import SI INNER JOIN RetrieveAccountNumber RAN ON SI.LeadID = RAN.LeadID;</code>
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>
This approach effectively updates the AccountNumber
field in Sales_Import
, avoiding null value issues by only updating rows where a matching LeadID
exists in both tables.
The above is the detailed content of How to Update Account Numbers in One SQL Table Based on Matching IDs in Another?. For more information, please follow other related articles on the PHP Chinese website!