Update query using data from another table

T

Traci

Hi. I've got data from two tables that I'm comparing to find rows that don't
match. Example.

MasterTable.SIM, MasterTable.Phone_Number
Table2.SIM, Table2.Phone Number

I've got a query that creates a result set showing all of the records in
Table2 and only the records from the MasterTable where the SIM fields match.
I'm now have the following results set:

Table2.SIM, MasterTable.SIM, Table2.Phone_Number, MasterTable.Phone_Number

Based on the result set above, I'm looking for a way to compare these Phone
number fields and then if they don't match, update Table2.Phone_Number with
the value in MasterTable.Phone_Number column.

Is this possible?
 
K

Ken Sheridan

Join the tables on the SIM columns and update the Table2.Phone_Number column
to the value of the MasterTable.Phone_Number column:

UPDATE Table2 INNER JOIN MasterTable
ON Table2.SIM = MasterTable.SIM
SET Table2.Phone_Number = MasterTable.Phone_Number;

You don't need to worry about identifying the numbers which don't match as,
by updating all rows, those that match are simply updated to their existing
values, while those that don't are updated to the values from MasterTable.

Ken Sheridan
Stafford, England
 
Top