40

I have a table in the name of ips as below:

CREATE TABLE `ips` (
 `id` int(10) unsigned NOT NULL DEFAULT '0',
 `begin_ip_num` int(11) unsigned DEFAULT NULL,
 `end_ip_num` int(11) unsigned DEFAULT NULL,
 `iso` varchar(3) DEFAULT NULL,
 `country` varchar(150) DEFAULT NULL
) ENGINE=InnoDB

Lets assume I have a countryid field on this table from country table which is as below:

CREATE TABLE `country` (
 `countryid` tinyint(3) unsigned NOT NULL AUTO_INCREMENT,
 `name` varchar(50) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
 `ordering` smallint(5) unsigned NOT NULL DEFAULT '0',
 `iso` char(2) NOT NULL,
 PRIMARY KEY (`countryid`)
) ENGINE=InnoDB

There is about 100,000 records in ips table. Is there any query for the following scenario:
Check if ips.iso is equal to country.iso, if it's equal then add country.coutryid to that record. I couldn't think of any way to do it. Do you have any idea how to do that?

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

  • You are a Genius you answer saved my time – humphrey Jan 7 '18 at 9:18
  • It amazes me how little code is necessary to perform this action! – Matt Cremeens Jun 24 at 13:31
32

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

  • 1
    It has since been fixed, seems... – rogerdpack Oct 6 '18 at 5:52
10

This syntax might be better readable

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

thanks @Cade, but I found a simple solution for it:

update ips set countryid=(select countryid from country where ips.iso=country.iso )
  • 5
    There is a difference in behavior to my version - your version will set it to NULL if it's not found, mine will not alter an existing value if it's not matched. This may or may not be desirable. Also the execution plan may differ depending upon the optimizer. – Cade Roux Jul 19 '12 at 13:28
  • @CadeRoux I didn't think about NULL part, thanks. – ALH Jul 19 '12 at 13:32
  • 1
    @john.locke It's probably not a problem - when you add the new column, I'm assuming it will be NULL and also a foreign key, so invalid entries wouldn't be allowed anyway. But it's hard to tell from what has been explicitly given in your question. – Cade Roux Jul 19 '12 at 13:34

protected by Community Oct 6 '18 at 11:21

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

Not the answer you're looking for? Browse other questions tagged or ask your own question.