[英]Selecting top n from subquery
假設我有一個桌子world(continent, country)
。
如何過濾此表以僅包括每個大陸的前五個國家的continent
和country
(字母順序)? 如果我只想按字母順序選擇第一個國家,則只需執行以下操作:
SELECT continent, country
FROM world x WHERE country = (
SELECT country
FROM world y
WHERE x.continent = y.continent
ORDER BY country asc
LIMIT 1
)
但是,如果我嘗試超越一個國家
SELECT continent, country
FROM world x WHERE country in (
SELECT country
FROM world y
WHERE x.continent = y.continent
ORDER BY country asc
LIMIT 5
)
然后拋出一個錯誤:
This version of MySQL doesn't yet support 'LIMIT & IN/ALL/ANY/SOME subquery'
我可以運行的替代查詢是什么?
對於每一行,請計算列表中位於該行之前的國家/地區:
SELECT continent, country
FROM world x
WHERE 5 > (
SELECT count(*)
FROM world y
WHERE x.continent = y.continent
AND x.country > y.country
)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.