Sams 在 10 分钟内自学 SQL - 关于 GROUP BY 的问题
我读了《Sams Teach Yourself SQL in 10 分钟第三版》一书,在第 10 课“分组数据”的“创建组”部分中,我无法理解以下内容:
“除了聚合计算语句之外,SELECT 语句中的每一列都必须出现在 GROUP BY 子句中。”
为什么?我尝试过这个,但我认为这不是真的。 例如,考虑一个表“世界”,其中包含“大陆”、“国家/地区”、“人口”列。
SELECT continent, country
FROM World
GROUP BY continent;
根据书上的说法,这应该会导致错误,对吗?但事实并非如此。我可以根据大陆对数据进行分组(因此我们的结果有 7 个大陆),并且在每个大陆旁边有一个随机的国家/地区名称。
像这样
continent country
North America Canada
South America Brazil
Europe France
Africa Cameroon
Asia Japan
Australia New Zealand
Antarctica TuxLand
i read the book "Sams Teach Yourself SQL in 10 minutes, Third Edition" and in the lesson 10 "Grouping Data", section "Creating Groups", i can't understand the following:
"Aside from the aggregate calculations statements, every column in your SELECT statement must be present in the GROUP BY clause."
Why? I tried this and i think that it is not true.
For example, consider a table 'World' with the columns 'continent', 'country', 'population'.
SELECT continent, country
FROM World
GROUP BY continent;
According to the book, this should lead to an error, right? But it doesn't. I can group my data depending on the continent (so we have at the results 7 continents) and next to each continent, a random country name.
Like this
continent country
North America Canada
South America Brazil
Europe France
Africa Cameroon
Asia Japan
Australia New Zealand
Antarctica TuxLand
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您很可能使用
MySQL
其中 允许在SELECT
子句中使用未分组和未聚合的表达式。这当然违反了标准。
这是为了通过
PRIMARY KEY
上的联接来简化GROUP BY
:通常,您必须将
a
中的所有列添加到a
中。 code>GROUP BY 子句或使用子查询。MySQL
允许您不这样做,因为对于PRIMARY KEY
的给定值(即分组)。You are most probably using
MySQL
which allows ungrouped and unaggregated expressions inSELECT
clause.This is violation of standard of course.
This is intended to simplify
GROUP BY
with joins on aPRIMARY KEY
:Normally, you would have either to add all columns from
a
into theGROUP BY
clause or use a subquery.MySQL
allows you not to do it since all values froma
are guaranteed to be the same for a given value of thePRIMARY KEY
(which is grouped on).这是正确的,并且在某些形式的 SQL(例如 MySQL)中不会产生错误。您可以选择对多个列使用 GROUP BY 语句,但这不是必需的。
This is correct and should produce no error in some forms of SQL such as MySQL. You may optionally use the GROUP BY statement on more than one column but it's not required.
GROUP BY 将列出指定列的第一个结果 - 因此在您的情况下,它将返回第一个国家/大陆对。
PostgreSQL 和 MySQL 允许这样做,使用一个字段作为分组依据。
本教程可能假设您应该在所有字段上使用 GROUP BY,这样您选择的内容就不会丢失任何数据 - 它将显示上面示例中的每个国家/大洲,但仅显示一次。
下面是一个示例表:
在您的第一个语句中:
输出将为:
请注意,其中一个亚洲行丢失了,尽管有所不同。
对两者使用 GROUP BY:
会产生:
GROUP BY will list the first result of the columns specified - so in your case, it would return the first country/continent pair.
PostgreSQL and MySQL allow this, using one field for the group by.
The tutorial probably assumes you should use GROUP BY on all fields so from what you select, you don't lose any data - it would show every country/continent in the above example, but only once.
Here's an example table:
In your first statement:
The output would be:
Notice one of the Asia rows was lost, despite being different.
Using a GROUP BY on both:
Would yield: