开发者

INSERT ... SELECT, WHERE

I have two tables, one called countries and one called country. Countries has bot开发者_如何学Goh countries and states, but no ISO for the countries. The country table has the country names with ISO, but no states. I'm trying to do an insert select to update a new ISO column in the countries table and populate it based on the country table (in other words, moving ISO from one table to the other).

Here's what I have:

INSERT countries (country_iso) SELECT country.iso FROM countries,country WHERE countries.name = country.printable_name

All this did, was put the iso's at the end of the countries table. It didn't use the WHERE countries.name = country.printable_name (which are the two columns that match).

Any ideas what I am doing wrong?

Thanks!


INSERT creates new rows. It looks like you want to UPDATE the existing rows instead.


You need to run an update, not an insert:

UPDATE countries set country_iso = country.iso
FROM countries INNER JOIN country on countries.name = country.printable_name

An insert creates new rows while an update modifies existing rows.


How about:

UPDATE countries,country SET countries.country_iso=country.iso WHERE countries.name=country.printable_name
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜