Home > Database > Mysql Tutorial > How to Update Account Numbers in One SQL Table Based on Matching IDs in Another?

How to Update Account Numbers in One SQL Table Based on Matching IDs in Another?

Mary-Kate Olsen
Release: 2025-01-23 00:50:11
Original
493 people have browsed it

How to Update Account Numbers in One SQL Table Based on Matching IDs in Another?

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

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!

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