MySQL 没有聚合功能的 GROUP BY 子句的任何原因?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2197693/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Any reason for GROUP BY clause without aggregation function?
提问by Niels Bom
I'm (thoroughly) learning SQL at the moment and came across the GROUP BY
clause.
我目前正在(彻底)学习 SQL 并遇到了该GROUP BY
子句。
GROUP BY
aggregates or groups the resultset according to the argument(s) you give it. If you use this clause in a query you can then perform aggregate functions on the resultset to find statistical information on the resultset like finding averages (AVG())
or frequency (COUNT())
.
GROUP BY
根据您提供的参数对结果集进行聚合或分组。如果在查询中使用此子句,则可以对结果集执行聚合函数以查找结果集的统计信息,例如查找平均值(AVG())
或频率(COUNT())
。
My question is: is the GROUP BY statement in any way useful without an accompanying aggregate function?
我的问题是:没有附带聚合函数的 GROUP BY 语句是否有用?
UpdateUsing GROUP BY
as a synonym for DISTINCT
is (probably) a bad idea because I suspect it is slower.
更新使用GROUP BY
的同义词DISTINCT
是(可能)是一个坏主意,因为我怀疑它是慢。
采纳答案by OMG Ponies
is the GROUP BY statement in any way useful without an accompanying aggregate function?
如果没有附带的聚合函数,GROUP BY 语句有任何用处吗?
Using DISTINCT
would be a synonym in such a situation, but the reason you'd want/have to define a GROUP BY
clause would be in order to be able to define HAVING
clause details.
DISTINCT
在这种情况下,使用将是同义词,但您想要/必须定义GROUP BY
子句的原因是为了能够定义HAVING
子句详细信息。
If you need to define a HAVING
clause, you haveto define a GROUP BY
- you can't do it in conjunction with DISTINCT
.
如果你需要定义一个HAVING
子句,你必须定义一个GROUP BY
- 你不能和DISTINCT
.
回答by Adriaan Stander
You can perform a DISTINCTselect by using a GROUP BYwithout any AGGREGATES.
您可以使用不带任何AGGREGATES的GROUP BY来执行DISTINCT选择。
回答by Ashish Agarwal
Group by can used in Two wayMajorly
1)in conjunctionwith SQL aggregation functions
2)to eliminateduplicate rows from a result set
Group by 可用于两种方式主要
1)结合SQL 聚合函数
2)从结果集中消除重复行
SO answer to your question lies in second part of USEs above described.
所以你的问题的答案在于上述用途的第二部分。
回答by Quassnoi
Note: everything below only applies to MySQL
注意:以下所有内容仅适用于 MySQL
GROUP BY
is guaranteedto return results in order, DISTINCT
is not.
GROUP BY
为保证,以便返回结果,DISTINCT
是不是。
GROUP BY
along with ORDER BY NULL
is of same efficiency as DISTINCT
(and implemented in the say way). If there is an index on the field being aggregated (or distinctified), both clauses use loose index scan over this field.
GROUP BY
与ORDER BY NULL
具有相同的效率DISTINCT
(并以口头方式实施)。如果聚合(或区分)字段上有索引,则两个子句都对该字段使用松散索引扫描。
In GROUP BY
, you can return non-grouped and non-aggregated expressions. MySQL
will pick any random values from from the corresponding group to calculate the expression.
在 中GROUP BY
,您可以返回非分组和非聚合表达式。MySQL
将从相应组中选取任意随机值来计算表达式。
With GROUP BY
, you can omit the GROUP BY
expressions from the SELECT
clause. With DISTINCT
, you can't. Every row returned by a DISTINCT
is guaranteed to be unique.
使用GROUP BY
,您可以省略子句中的GROUP BY
表达式SELECT
。用DISTINCT
,你不能。a 返回的每一行都DISTINCT
保证是唯一的。
回答by Amirshk
It is used for more then just aggregating functions.
它不仅用于聚合函数。
For example, consider the following code:
例如,考虑以下代码:
SELECT product_name, MAX('last_purchased') FROM products GROUP BY product_name
This will return only 1 result per product, but with the latest updated value of that records.
这将只返回每个产品的 1 个结果,但具有该记录的最新更新值。