在一次查询中获取所有国家/地区的州和城市
Posted
技术标签:
【中文标题】在一次查询中获取所有国家/地区的州和城市【英文标题】:Get all countries states and cities in one query 【发布时间】:2017-09-25 03:35:23 【问题描述】:我有三个包含以下数据的表:
countries
+-----+----------+
| id | name |
+-----+----------+
| 1 | country1 |
| 7 | country2 |
+-----+----------+
states
+-----+----------+------------+
| id | name | country_id |
+-----+----------+------------+
| 3 | state1 | 1 |
| 9 | state2 | 7 |
| 11 | state3 | 1 |
| 17 | state4 | 1 |
+-----+----------+------------+
cities
+-----+----------+------------+
| id | name | state_id |
+-----+----------+------------+
| 5 | city1 | 3 |
| 6 | city2 | 9 |
| 22 | city3 | 9 |
| 24 | city4 | 17 |
| 25 | city5 | 11 |
| 26 | city6 | 11 |
+-----+----------+------------+
我正在尝试选择所有数据,以便生成以下输出:
+-----+---------------------------+--------+-------+
| id | table_name | country | state | city |
+-----+---------------------------+--------+-------+
| 1 | countries | country1 | | |
| 3 | states | country1 | state1 | |
| 5 | cities | country1 | state1 | city1 |
| 11 | states | country1 | state3 | |
| 25 | cities | country1 | state3 | city5 |
| 26 | cities | country1 | state3 | city6 |
| 17 | states | country1 | state4 | |
| 24 | cities | country1 | state4 | city4 |
| 7 | countries | country2 | | |
| 9 | states | country2 | state2 | |
| 5 | cities | country2 | state2 | city2 |
| 5 | cities | country2 | state2 | city3 |
+-----+---------------------------+--------+-------+
我知道这很有挑战性,但我想知道是否可以使用 SELECT 生成这样的结果,还是只能以编程方式完成?谢谢!
【问题讨论】:
这和js有什么关系? 去看看joins 不,你不能使用联合,联合要求所有表中的列数相同 @Jim Macaulay 如果您提供虚拟值,您可以.. How can I join multiple SQL tables using the IDs?的可能重复 【参考方案1】:在我看到@nos 已经提供了几乎相同的答案之前,我已经写了这个。此版本添加字段别名,根据 OP 的示例输出对数据进行排序并避免拼写错误。
select
c.id,
'countries' as table_name,
c.name as country,
'' as state,
'' as city
from countries c
union
select
s.id,
'states' as table_name,
c.name as country,
s.name as state,
'' as city
from countries c
JOIN states s ON c.id = s.country_id
union
select
ci.id,
'cities' as table_name,
c.name as country,
s.name as state,
ci.name as city
from countries c
JOIN states s ON c.id = s.country_id
JOIN cities ci ON s.id = ci.state_id
order by country, state, city
【讨论】:
【参考方案2】:你需要这 3 条 SQL 语句:
所有城市:
SELECT cit.id, 'cities', cont.name, st.name, cit.name
FROM countries cont
INNER JOIN states st ON cont.id = st.country_id
INNER JOIN join cities cit ON st.id = cit.state_id
所有状态:
SELECT stat.id, 'states', cont.name, st.name, ''
FROM countries cont
INNER JOIN states st ON cont.id = st.country_id
所有国家;
SELECT cont.id, 'countries', cont.name, '', '' FROM countries cont
然后你就可以像这样组合它们了
SELECT cit.id, 'cities', cont.name, st.name, cit.name
FROM countries cont
INNER JOIN states st ON cont.id = st.country_id
INNER JOIN join cities cit ON st.id = cit.state_id
UNION ALL
SELECT stat.id, 'states', cont.name, st.name, ''
FROM countries cont
INNER JOIN states st ON cont.id = st.country_id
UNION ALL
SELECT cont.id, 'countries', cont.name, '', '' FROM countries cont
【讨论】:
【参考方案3】:您可以使用以下查询
SELECT C.id, C.name AS table_name, 'country1' AS country, S.name AS state, CI.city
FROM COUNTRIES C
FULL OUTER JOIN STATES S
ON (C.ID = S.ID)
FULL OUTER JOIN CITIES CI
ON (C.ID=CI.ID);
Or you can use
SELECT C.id, C.name AS table_name, 'country1' AS country, S.name AS state, CI.city
FROM COUNTRIES C, STATES S, CITIES CI
WHERE C.ID = (+)S.ID AND
C.ID=(+)CI.ID;
【讨论】:
我认为 mysql 不支持完全外连接。 (+)S.ID 是一个无效的结构。有趣的是,哪种 sql 支持这一点?以上是关于在一次查询中获取所有国家/地区的州和城市的主要内容,如果未能解决你的问题,请参考以下文章
如何/在哪里获取非美国国家/地区的州、省和行政区的 geoJSON 数据? [关闭]