11 April 2010

Update mySQL table with values from another table

Suppose we have two tables myCustomers and allCustomers. We can update the value of table myCustomers based on the value that is common to both tables.

Example

UPDATE myCustomers , allCustomers
SET myCustomers.name = allCustomers.name
WHERE myCustomers.email = allCustomers.email;

What we did here is update all the names of table 'myCustomers' with the names of table 'allCustomers' where their email matches.

Table 'myCustomers' can be a smaller table that stores a small amount of info and table 'allCustomers' can be a reference master table.

No comments:

Post a Comment