我有这个查询:
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;