Sams 在 10 分钟内自学 SQL - 关于 GROUP BY 的问题

发布于 2024-10-12 10:46:43 字数 636 浏览 3 评论 0原文

我读了《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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

再可℃爱ぅ一点好了 2024-10-19 10:46:43

您很可能使用 MySQL 其中 允许在 SELECT 子句中使用未分组和未聚合的表达式

这当然违反了标准。

这是为了通过 PRIMARY KEY 上的联接来简化 GROUP BY

SELECT  a.*, SUM(b.value)
FROM    a
JOIN    b
ON      b.a_id = a.id
GROUP BY
        a.id

通常,您必须将 a 中的所有列添加到 a 中。 code>GROUP BY 子句或使用子查询。

MySQL 允许您不这样做,因为对于 PRIMARY KEY 的给定值(即分组)。

You are most probably using MySQL which allows ungrouped and unaggregated expressions in SELECT clause.

This is violation of standard of course.

This is intended to simplify GROUP BY with joins on a PRIMARY KEY:

SELECT  a.*, SUM(b.value)
FROM    a
JOIN    b
ON      b.a_id = a.id
GROUP BY
        a.id

Normally, you would have either to add all columns from a into the GROUP BY clause or use a subquery.

MySQL allows you not to do it since all values from a are guaranteed to be the same for a given value of the PRIMARY KEY (which is grouped on).

江湖彼岸 2024-10-19 10:46:43

这是正确的,并且在某些形式的 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.

吃兔兔 2024-10-19 10:46:43

GROUP BY 将列出指定列的第一个结果 - 因此在您的情况下,它将返回第一个国家/大陆对。

PostgreSQL 和 MySQL 允许这样做,使用一个字段作为分组依据。

本教程可能假设您应该在所有字段上使用 GROUP BY,这样您选择的内容就不会丢失任何数据 - 它将显示上面示例中的每个国家/大洲,但仅显示一次。

下面是一个示例表:

Continent      |  Country     | Random_Field
---------------------------------------------
North America     Canada          Cake
North America     Canada          Dog
South America     Brazil          Cat
Europe            France          Frog
Africa            Cameroon        House
Asia              Japan           Gadget
Asia              India           Dance
Australia         New Zealand     Frodo
Antarctica        TuxLand         Linux

在您的第一个语句中:

SELECT continent, country
FROM World
GROUP BY continent;

输出将为:

Continent      |  Country     
--------------------------
North America     Canada   
South America     Brazil 
Europe            France  
Africa            Cameroon   
Asia              Japan    
Australia         New Zealand 
Antarctica        TuxLand  

请注意,其中一个亚洲行丢失了,尽管有所不同。

对两者使用 GROUP BY:

SELECT continent, country
FROM World
GROUP BY continent, country;

会产生:

   Continent      |  Country     
    -----------------------------
    North America     Canada   
    South America     Brazil    
    Europe            France    
    Africa            Cameroon    
    Asia              Japan      
    Asia              India      
    Australia         New Zealand 
    Antarctica        TuxLand 

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:

Continent      |  Country     | Random_Field
---------------------------------------------
North America     Canada          Cake
North America     Canada          Dog
South America     Brazil          Cat
Europe            France          Frog
Africa            Cameroon        House
Asia              Japan           Gadget
Asia              India           Dance
Australia         New Zealand     Frodo
Antarctica        TuxLand         Linux

In your first statement:

SELECT continent, country
FROM World
GROUP BY continent;

The output would be:

Continent      |  Country     
--------------------------
North America     Canada   
South America     Brazil 
Europe            France  
Africa            Cameroon   
Asia              Japan    
Australia         New Zealand 
Antarctica        TuxLand  

Notice one of the Asia rows was lost, despite being different.

Using a GROUP BY on both:

SELECT continent, country
FROM World
GROUP BY continent, country;

Would yield:

   Continent      |  Country     
    -----------------------------
    North America     Canada   
    South America     Brazil    
    Europe            France    
    Africa            Cameroon    
    Asia              Japan      
    Asia              India      
    Australia         New Zealand 
    Antarctica        TuxLand 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文