[英]MySQL limit & in wont work together
我有3张桌子,我正在尝试获得10种最常用的语言以及使用该语言的国家/地区的数量。 我的表是:
City: Name|Population|CountryCode
Country: Name|Code
CoutryLanguage: Language|CountryCode
为了获得十大语言,我使用了每个城市的人口并将其与乡村语言结合在一起:
SELECT cl.language
FROM City ci INNER JOIN CountryLanguage cl ON ci.CountryCode = cl.CountryCode
GROUP BY cl.Language
order by sum(ci.population) desc limit 10
我的下一个逻辑是使用新创建的表中的语言加入国家/地区:
select co.Name, cl.Language
from Country co Inner join CountryLanguage cl on co.Code = cl.CountryCode
where cl.Language in (SELECT cl.language
FROM City ci INNER JOIN CountryLanguage cl ON ci.CountryCode = cl.CountryCode
GROUP BY cl.Language
order by sum(ci.population) desc limit 10)
这给了我一个奇怪的错误,我可以在没有LIMIT 10
情况下使用它,但是它只列出了所有国家和所有语言。 有没有办法解决这个问题?? 我看到的问题很少,但没有一个是很清楚的。 您介意向我解释此错误吗? 我无法升级sql版本。 提前致谢
我将语言数量限制为10种,而不是整个表格
请尝试这一步,对所有来回的请求感到抱歉,但我没有安装mysql,只需在此处编写SQL。
select co.Name, cl.Language
from Country co
Inner join CountryLanguage cl on co.Code = cl.CountryCode
inner join (
SELECT language from (
SELECT DISTINCT cl.language
FROM City ci INNER JOIN CountryLanguage cl ON ci.CountryCode = cl.CountryCode
GROUP BY cl.Language
order by sum(ci.population) desc
) as lng_tbl
limit 10
) aux_tbl
on aux_tbl.Language = cl.Language
查看子查询,因为它是在运行时/内存中创建的表,在这种情况下,您将获得以下表:
SELECT language from (
SELECT DISTINCT cl.language
FROM City ci
INNER JOIN CountryLanguage cl
ON ci.CountryCode = cl.CountryCode
GROUP BY cl.Language
order by sum(ci.population) desc
) as lng_tbl
limit 10
>>> | language |
>>> | English |
>>> | Portuguese |
>>> | ... |
之后,只需将其包装在()中,并为其命名为aux_tbl
即可在aux_tbl
和普通表中使用它。 作为练习,请尝试摆脱()中的外部选择语言lng_tbl。
只需使用如下临时表替换子查询:
CREATE TEMPORARY TABLE tmp_lang (key(Language)) SELECT cl.language
FROM City ci INNER JOIN CountryLanguage cl ON
ci.CountryCode = cl.CountryCode
GROUP BY cl.Language
order by sum(ci.population) desc limit 10;
select co.Name, cl.Language
from Country co Inner join CountryLanguage cl on co.Code = cl.CountryCode
join
tmp_lang tl on c1.Language = tl.Language;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.