私はこのクエリを持っています:
SELECT wc.city, wc.country_id, wc.latitude, wc.longitude FROM
world_cities wc
-- left join states s on wc.country_id = s.country_id
where wc.city = 'rosemount'
それで6つの結果(私が欲しいもの)を取得します。3行目のコメントを外すと、150の結果が得られます。3行目のコメントを外すと、結果が増えますが、それでも6行しか得られないのではないでしょうか。ここで何かが足りないような気がします...
州
mysql> describe states;
+------------+---------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------+---------------------+------+-----+---------+----------------+
| state_id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| country_id | tinyint(3) unsigned | YES | | NULL | |
| state_name | char(15) | YES | | NULL | |
+------------+---------------------+------+-----+---------+----------------+
3 rows in set (0.00 sec)
世界の都市
mysql> describe world_cities;
+-------------+---------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+---------------------+------+-----+---------+----------------+
| city_id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| state_id | int(11) | YES | | 0 | |
| country_id | tinyint(3) unsigned | YES | MUL | NULL | |
| country | char(2) | YES | | NULL | |
| city | char(60) | YES | MUL | NULL | |
| accent_city | char(60) | YES | | NULL | |
| region | int(11) | YES | | NULL | |
| population | int(11) | YES | | NULL | |
| latitude | decimal(9,6) | YES | | NULL | |
| longitude | decimal(9,6) | YES | | NULL | |
+-------------+---------------------+------+-----+---------+----------------+
9 rows in set (0.01 sec)
編集
これを実行しました。world_citiesテーブルのstate_idがかなり更新されます。
update world_cities wc
left join zipcodes z on wc.city = z.city
left join cities c on z.state = c.city_name
left join states s on z.state = s.state_name
set wc.state_id = s.state_id
where wc.country_id = 236
and abs(round(z.lat, 2) - round(wc.latitude, 2)) between 0 and 1
and abs(round(z.log, 2) - round(wc.longitude, 2)) between 0 and 1;