任何人都可以帮助我以下内容:
有些国家的人口是其邻国(在同一地区)的三倍以上.给国家和地区.
我的尝试:
select x.name, x.region
from bbc x
where x.population >all
(select population*3
from bbc y
where y.region = x.region)
Run Code Online (Sandbox Code Playgroud)
语法是正确的但没有返回记录(应该返回3行)
查找属于所有人口少于25000000的地区的每个国家/地区.显示姓名,地区和人口.
我的尝试:
select name, region, population
from bbc
where region not in
(select distinct region from bbc
where population >= 25000000)
Run Code Online (Sandbox Code Playgroud)
我用"不在".有没有办法使用"in"?
小智 8
SELECT name, region
FROM bbc x
WHERE population/3 >= ALL
(SELECT population
FROM bbc y
WHERE y.region=x.region
AND x.name != y.name)
Run Code Online (Sandbox Code Playgroud)
为了第一 :
你必须分工.第一步,找到一个国家的邻居.这必须是自动加入:
SELECT *
FROM bbc country
INNER JOIN bbc neighbours
ON country.region = neighbours.region
AND country.name != neighbours.name
Run Code Online (Sandbox Code Playgroud)
不要忘记将自己的国家排除在邻居之外!
其次,您可以计算一个国家的邻居拥有合适人口的数量:
sum(CASE WHEN country.population > neighbours.population * 3 THEN 1 ELSE 0 END)
(Group by country !)
Run Code Online (Sandbox Code Playgroud)
与总数相比,你就完成了!
SELECT countryName
FROM
(
SELECT sum(CASE WHEN country.population > neighbours.population * 3 THEN 1 ELSE 0 END) as okNeighbours,
count(*) as totalNeighbours
country.name as countryName
FROM bbc country
INNER JOIN bbc neighbours
ON country.region = neighbours.region
AND country.name != neighbours.name
GROUP BY country.name
)
WHERE totalNeighbours = okNeighbours
Run Code Online (Sandbox Code Playgroud)
对于第二个:
SELECT name, region, population
FROM bbc
WHERE region IN (
SELECT region
FROM bbc
GROUP BY region
HAVING SUM(CASE WHEN population >= 25000000 THEN 1 ELSE 0 END) = 0
)
Run Code Online (Sandbox Code Playgroud)
其他一些解决方案,为了兴趣而增加.
第一个查询:
SELECT name,
region
FROM bbc x
WHERE population >
-- this sub query finds each neighbour (not including itself) and returns the max populations multiplied by 3
(SELECT 3 * MAX(population)
FROM bbc y
WHERE x.region = y.region
AND x.name <> y.name)
Run Code Online (Sandbox Code Playgroud)
第二个查询:
SELECT name,
region,
population
FROM bbc x
WHERE population < ALL
-- the ALL keyword allows comparison to be made against all the values in a list
-- this sub query finds each country that belongs to a region with populations less than 25 million and returns this as a list
(SELECT population
FROM bbc y
WHERE y.region = x.region
AND population > 25000000)
Run Code Online (Sandbox Code Playgroud)
小智 6
埃拉德,您的第一个答案几乎是正确的,只是缺少一个非常关键的组成部分:
SELECT x.name, x.continent
FROM world x
WHERE x.population >ALL(SELECT population*3
FROM world y
WHERE y.continent = x.continent
AND x.name<>y.name)
Run Code Online (Sandbox Code Playgroud)
您会看到在执行子查询时要检查x.population> 3 *(同一大陆的y.populations),您必须指定不对同一国家/地区进行检查;否则,您将要检查x> 3x,这在数学上是不可能的。
小智 5
SELECT name, region
FROM bbc x
WHERE population > 3 *
(SELECT population
FROM bbc y
WHERE x.region=y.region
ORDER BY population DESC limit 1,1)
Run Code Online (Sandbox Code Playgroud)
您要照顾的人口是第二高人口价值的3倍,即限制1,1。第二个问题是缺少0,然后是正确的。
SELECT name, region, population
FROM bbc x
WHERE (SELECT SUM(population)
FROM bbc y
WHERE x.region=y.region) < 250000000
Run Code Online (Sandbox Code Playgroud)