假设我有一个表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'
我可以运行什么替代查询?
答案 0 :(得分:1)
对于每一行,计算列表中的国家/地区数量:
SELECT continent, country
FROM world x
WHERE 5 > (
SELECT count(*)
FROM world y
WHERE x.continent = y.continent
AND x.country > y.country
)