我有以下表格:
查找属于所有人口均少于25000000的大陆的每个国家/地区。
SELECT name,
continent,
population
FROM world
WHERE continent IN
(SELECT continent
FROM world t
WHERE 25000000 > ALL
(SELECT population
FROM world
WHERE continent = t.continent))
我是否可以使用不需要如此多嵌套级别的关键字或函数以更好的方式编写此内容?
答案 0 :(得分:0)
SELECT w1.name,w1.continent,w1.population
FROM world w1
INNER JOIN
(
SELECT continent, SUM(population) as continent_population
FROM world
GROUP BY continent
) w2 ON w2.continent = w1.continent
WHERE w2.continent_population < 25000000
..或..
SELECT name,continent,population
FROM world w1
WHERE
EXISTS
(
SELECT 1
FROM world w2
WHERE w2.continent = w1.continent
GROUP BY continent
HAVING SUM(population) < 25000000
)
答案 1 :(得分:0)
要求:
查找属于所有人口的大陆的每个国家/地区 小于25000000。
我能想到的最直接的方式:
SELECT name, continent, population FROM world
WHERE continent in (
SELECT continent FROM world
GROUP BY continent
HAVING SUM(population) < 25000000)
答案 2 :(得分:0)
您正在寻找所有国家人口少于25,000,000的大陆。所以按大洲分组并保留最大人口不超过这个数量的那些。
SELECT
name,
continent,
population
FROM world
WHERE continent IN
(
SELECT continent
FROM world
GROUP BY continent
HAVING MAX(population) < 25000000
);