How to update one table based on another table's values on the fly?

UPDATE ips INNER JOIN country
    ON ips.iso = country.iso
SET ips.countryid = country.countryid

Using MySQL update multiple table syntax:

14.2.11 UPDATE Syntax

Note that you have two different lengths and data types on your iso columns. There are, in fact, two separate sets of ISO codes, 2-letter and 3-letter, so you may not in reality be able to join these columns:

ISO 3166-1

The join condition USING (iso) instead of ON ips.iso = country.iso works too.


@Cade Roux's solution gives me a syntax error, the correct one for mysql 5.5.29 is:

UPDATE ips 
INNER JOIN country
    ON ips.iso = country.iso
SET ips.countryid = country.countryid

without the "FROM" keyword.


This syntax might be better readable

UPDATE country p, ips pp
SET pp.countryid = p.countryid
WHERE pp.iso = p.iso

Tags:

Mysql

Update