Sql,嵌套 select
Sql, nested select
我正在学习如何在另一个 select 中做一个 select,我找到了一个解决方案,但它太长了。这个解决方案可以更短吗?
我想要这个查询:
select continent, name
from world x
where name =
(SELECT min(name) FROM world
WHERE continent = 'Africa')
or name =
(SELECT min(name) FROM world
WHERE continent = 'Asia')
or name =
(SELECT min(name) FROM world
WHERE continent = 'Caribbean')
or name =
(SELECT min(name) FROM world
WHERE continent = 'Eurasia')
or name =
(SELECT min(name) FROM world
WHERE continent = 'Europe')
or name =
(SELECT min(name) FROM world
WHERE continent = 'North America')
or name =
(SELECT min(name) FROM world
WHERE continent = 'Oceania')
or name =
(SELECT min(name) FROM world
WHERE continent = 'South America')
order by continent
limit 8
写成这样:
SELECT continent, name, area FROM world x
WHERE area >= ALL
(SELECT max(area) FROM world y
WHERE y.continent = x.continent
AND area > 0)
还有谁能解释一下第二个查询,它是如何工作的,谢谢。
试试这个
SELECT Continent, Name
FROM World x
WHERE NAME IN (SELECT MIN(Name) FROM world WHERE Continent IN ('Africa', 'Asia', 'Caribbean', 'Eurasia', 'Europe', 'North America', 'Oceania', 'South America') GROUP BY Continent)
ORDER BY Continent
LIMIT 8
我正在学习如何在另一个 select 中做一个 select,我找到了一个解决方案,但它太长了。这个解决方案可以更短吗?
我想要这个查询:
select continent, name
from world x
where name =
(SELECT min(name) FROM world
WHERE continent = 'Africa')
or name =
(SELECT min(name) FROM world
WHERE continent = 'Asia')
or name =
(SELECT min(name) FROM world
WHERE continent = 'Caribbean')
or name =
(SELECT min(name) FROM world
WHERE continent = 'Eurasia')
or name =
(SELECT min(name) FROM world
WHERE continent = 'Europe')
or name =
(SELECT min(name) FROM world
WHERE continent = 'North America')
or name =
(SELECT min(name) FROM world
WHERE continent = 'Oceania')
or name =
(SELECT min(name) FROM world
WHERE continent = 'South America')
order by continent
limit 8
写成这样:
SELECT continent, name, area FROM world x
WHERE area >= ALL
(SELECT max(area) FROM world y
WHERE y.continent = x.continent
AND area > 0)
还有谁能解释一下第二个查询,它是如何工作的,谢谢。
试试这个
SELECT Continent, Name
FROM World x
WHERE NAME IN (SELECT MIN(Name) FROM world WHERE Continent IN ('Africa', 'Asia', 'Caribbean', 'Eurasia', 'Europe', 'North America', 'Oceania', 'South America') GROUP BY Continent)
ORDER BY Continent
LIMIT 8