0

为什么这三个查询如此不同?根据 MySQL 文档,他们不应该都做同样的事情吗?

select cc, max(c) as "Most Official Billingual Count" from (
  select CountryCode as cc, count(*) as c from countrylanguage where isofficial = "T"
  group by cc
) t group by cc

以这种格式返回多行:CountryCode Official_Language_Count_Blabla

而如果我删除一组...

select cc, max(c) as "Most Official Billingual Count" from (
  select CountryCode as cc, count(*) as c from countrylanguage where isofficial = "T"

) t group by cc

它返回第一个国家代码以及总数:ABW 238

还有这个:

select cc, max(c) as "Most Official Billingual Count" from (
  select CountryCode as cc, count(*) as c from countrylanguage where isofficial = "T"
  group by cc
) t 

将返回如下内容: ABW 4

我对这整个群体感到非常困惑。

内部查询

  select CountryCode as cc, count(*) as c from countrylanguage where isofficial = "T

应该返回所有国家代码并计算重复的国家代码吗?对?但它不会那样做。似乎我必须排第一个队列才能获得所需的效果,这对我来说非常混乱

4

1 回答 1

1

您的子查询:

select CountryCode as cc, count(*) as c from countrylanguage where isofficial = "T"

除非您没有通过相应的标准对记录进行分组,否则没有任何意义。您需要对记录进行分组,因为WHERE子句仅指示选择标准。请参阅此澄清视频(DBMS 是 Oracle 那里,但没关系)以了解GROUP BY。另外(我更喜欢阅读),您可以找到很多相关的文章,例如this

因此,如果您需要MAX在计数之间进行查找 - 您需要首先GROUP BY在子查询中组合您的组。

于 2013-08-29T08:08:46.333 回答