Home >Database >Mysql Tutorial >mysql functions used simultaneously with the GROUP BY clause
1. GROUP BY (aggregation) function
This chapter discusses the group (set) function for a set of numerical operations. Unless otherwise noted, group functions ignore NULL values.
If you use a group function in a statement that does not contain a ROUP BY clause, it is equivalent to grouping all rows.
AVG([DISTINCT] expr)
Returns the average value of expr. The DISTINCT option can be used to return the average of distinct values of expr.
If no matching row is found, AVG() returns NULL.
mysql> SELECT student_name, AVG(test_score) -> FROM student -> GROUP BY student_name;
BIT_AND(expr)
Returns the bitwise AND of all bits in expr. Calculations are performed with a precision of 64 bits (BIGINT).
If no matching row is found, this function returns 18446744073709551615. (This is an unsigned BIGINT value with all bits set to 1).
BIT_OR(expr)
Returns the bitwise OR of all bits in expr. Calculations are performed with a precision of 64 bits (BIGINT).
If no matching row is found, the function returns 0.
BIT_XOR(expr)
Returns the bitwise XOR of all bits in expr. Calculations are performed with a precision of 64 bits (BIGINT).
If no matching row is found, the function returns 0.
COUNT(expr)
Returns the number of non-NULL values in the rows retrieved by the SELECT statement.
If no matching row is found, COUNT() returns 0.
mysql> SELECT student.student_name,COUNT(*)-> FROM student,course-> WHERE student.student_id=course.student_id-> GROUP BY student_name;
COUNT(*) is slightly different in that it returns the number of retrieved rows, regardless of whether they contain NULL values.
SELECT is retrieved from a table without retrieving other columns, and there is no WHERE clause, COUNT(*) is optimized for the fastest return speed. For example:
mysql> SELECT COUNT(*) FROM student;
This optimization only applies to MyISAM tables, because these table types store the exact number of records returned by a function and are very easy to access. For transactional storage engines (InnoDB, BDB), storing an exact number of rows is more problematic because multiple transactions may occur, each of which may have an impact on the number of rows.
COUNT(DISTINCT expr,[expr...])
Returns the number of distinct non-NULL values.
If no matching item is found, COUNT(DISTINCT) returns 0.
mysql> SELECT COUNT(DISTINCT results) FROM student;
In MySQL, you get the number of different expression combinations that do not contain NULL by giving a list of expressions. In standard SQL, you would have to concatenate all expressions in COUNT(DISTINCT ...).
GROUP_CONCAT(expr)
This function returns a string result with non-NULL values from the concatenation of a group. Its complete syntax is as follows:
GROUP_CONCAT([DISTINCT] expr [,expr ...] [ORDER BY {unsigned_integer | col_name | expr} [ASC | DESC] [,col_name ...]] [SEPARATOR str_val]) mysql> SELECT student_name,-> GROUP_CONCAT(test_score)-> FROM student-> GROUP BY student_name; Or: mysql> SELECT student_name,-> GROUP_CONCAT(DISTINCT test_score-> ORDER BY test_score DESC SEPARATOR ' ')-> FROM student-> GROUP BY student_name;
In MySQL, you can get the concatenated value of a combination of expressions. You can use DISTINCT to remove duplicate values. If you want to sort multiple result values, you should use the ORDER BY clause. To sort in reverse order, add the DESC (descending) keyword to the column name you want to sort with the ORDER BY clause. The default order is ascending; it can be specified explicitly using ASC. SEPARATOR is followed by the string value that should be inserted into the middle of the resulting value. The default is comma (‘,’). By specifying SEPARATOR '' you can remove all separators.
Using the group_concat_max_len system variable, you can set the maximum length allowed. The syntax for this operation in the program is as follows, where val is an unsigned integer:
SET [SESSION | GLOBAL] group_concat_max_len = val;
If the maximum length has been set, the result will be cut to this maximum length.
MIN([DISTINCT] expr), MAX([DISTINCT] expr)
Return the minimum and maximum values of expr. The value of MIN() and MAX() can be a string argument; in these cases, they return the minimum or maximum string value. The DISTINCT keyword can be used to find the minimum or maximum value for different values of expr, however, this produces the same results as omitting DISTINCT.
If no matching row is found, MIN() and MAX() return NULL.
mysql> SELECT student_name, MIN(test_score), MAX(test_score)-> FROM student-> GROUP BY student_name;
For MIN(), MAX(), and other aggregate functions, MySQL currently compares ENUM and SET columns based on their string values rather than the relative position of the strings in the collection. This is different from the way ORDER BY compares the two. This should be improved in future versions of MySQL.
STD(expr) STDDEV(expr)
Returns the population standard deviation of expr. This is an extension of standard SQL. The STDDEV() form of this function is used to provide compatibility with Oracle. The standard SQL function STDDEV_POP() can be used instead.
If no matching row is found, these functions return NULL.
STDDEV_POP(expr)
Returns the population standard deviation of expr (the square root of VAR_POP()). You can also use STD() or STDDEV(), which have the same meaning but are not standard SQL.
若找不到匹配的行,则STDDEV_POP()返回 NULL。
STDDEV_SAMP(expr)
返回expr 的样本标准差 ( VAR_SAMP()的平方根)。
若找不到匹配的行,则STDDEV_SAMP() 返回 NULL 。
SUM([DISTINCT] expr)
返回expr 的总数。 若返回集合中无任何行,则 SUM() 返回NULL。DISTINCT 关键词可用于 MySQL 5.1 中,求得expr不同值的总和。
若找不到匹配的行,则SUM()返回 NULL。
VAR_POP(expr)
返回expr 总体标准方差。它将行视为总体,而不是一个样本, 所以它将行数作为分母。你也可以使用 VARIANCE(),它具有相同的意义然而不是 标准的 SQL。
若找不到匹配的项,则VAR_POP()返回NULL。
VAR_SAMP(expr)
返回expr 的样本方差。更确切的说,分母的数字是行数减去1。
若找不到匹配的行,则VAR_SAMP()返回NULL。
VARIANCE(expr)
返回expr 的总体标准方差。这是标准SQL 的延伸。可使用标准SQL 函数 VAR_POP() 进行代替。
若找不到匹配的项,则VARIANCE()返回NULL。
2. GROUP BY修改程序
GROUP BY子句允许一个将额外行添加到简略输出端 WITH ROLLUP 修饰符。这些行代表高层(或高聚集)简略操作。ROLLUP 因而允许你在多层分析的角度回答有关问询的问题。例如,它可以用来向OLAP (联机分析处理) 操作提供支持。
设想一个名为sales 的表具有年份、国家、产品及记录销售利润的利润列:
CREATE TABLE sales ( year INT NOT NULL, country VARCHAR(20) NOT NULL, product VARCHAR(32) NOT NULL, profit INT );
可以使用这样的简单GROUP BY,每年对表的内容做一次总结:
mysql> SELECT year, SUM(profit) FROM sales GROUP BY year;+------+-------------+ | year | SUM(profit) | +------+-------------+ | 2000 | 4525 | | 2001 | 3010 | +------+-------------+
这个输出结果显示了每年的总利润, 但如果你也想确定所有年份的总利润,你必须自己累加每年的单个值或运行一个加法询问。
或者你可以使用 ROLLUP, 它能用一个问询提供双层分析。将一个 WITH ROLLUP修饰符添加到GROUP BY 语句,使询问产生另一行结果,该行显示了所有年份的总价值:
mysql> SELECT year, SUM(profit) FROM sales GROUP BY year WITH ROLLUP;+------+-------------+ | year | SUM(profit) | +------+-------------+ | 2000 | 4525 | | 2001 | 3010 | | NULL | 7535 | +------+-------------+
总计高聚集行被年份列中的NULL值标出。
当有多重 GROUP BY 列时,ROLLUP产生的效果更加复杂。这时,每次在除了最后一个分类列之外的任何列出现一个 “break” (值的改变) ,则问讯会产生一个高聚集累计行。
例如,在没有 ROLLUP的情况下,一个以年、国家和产品为基础的关于 sales 表的一览表可能如下所示:
mysql> SELECT year, country, product, SUM(profit)-> FROM sales-> GROUP BY year, country, product;+------+---------+------------+-------------+ | year | country | product | SUM(profit) | +------+---------+------------+-------------+ | 2000 | Finland | Computer | 1500 | | 2000 | Finland | Phone | 100 | | 2000 | India | Calculator | 150 | | 2000 | India | Computer | 1200 | | 2000 | USA | Calculator | 75 | | 2000 | USA | Computer | 1500 | | 2001 | Finland | Phone | 10 | | 2001 | USA | Calculator | 50 | | 2001 | USA | Computer | 2700 | | 2001 | USA | TV | 250 | +------+---------+------------+-------------+
表示总值的输出结果仅位于年/国家/产品的分析级别。当添加了 ROLLUP后, 问询会产生一些额外的行:
mysql> SELECT year, country, product, SUM(profit) -> FROM sales -> GROUP BY year, country, product WITH ROLLUP;+------+---------+------------+-------------+ | year | country | product | SUM(profit) | +------+---------+------------+-------------+ | 2000 | Finland | Computer | 1500 | | 2000 | Finland | Phone | 100 | | 2000 | Finland | NULL | 1600 | | 2000 | India | Calculator | 150 | | 2000 | India | Computer | 1200 | | 2000 | India | NULL | 1350 | | 2000 | USA | Calculator | 75 | | 2000 | USA | Computer | 1500 | | 2000 | USA | NULL | 1575 | | 2000 | NULL | NULL | 4525 | | 2001 | Finland | Phone | 10 | | 2001 | Finland | NULL | 10 | | 2001 | USA | Calculator | 50 | | 2001 | USA | Computer | 2700 | | 2001 | USA | TV | 250 | | 2001 | USA | NULL | 3000 | | 2001 | NULL | NULL | 3010 | | NULL | NULL | NULL | 7535 | +------+---------+------------+-------------+
对于这个问询, 添加ROLLUP 子句使村输出结果包含了四层分析的简略信息,而不只是一个下面是怎样解释 ROLLUP输出:
一组给定的年份和国家的每组产品行后面, 会产生一个额外的总计行, 显示所有产品的总值。这些行将产品列设置为 NULL。
一组给定年份的行后面,会产生一个额外的总计行,显示所有国家和产品的总值。这些行将国家和产品列设置为 NULL。
最后, 在所有其它行后面,会产生一个额外的总计列,显示所有年份、国家及产品的总值。 这一行将年份、国家和产品列设置为 NULL。
使用ROLLUP 时的其它注意事项
以下各项列出了一些MySQL执行ROLLUP的特殊状态:
当你使用 ROLLUP时, 你不能同时使用 ORDER BY子句进行结果排序。换言之, ROLLUP 和ORDER BY 是互相排斥的。然而,你仍可以对排序进行一些控制。在 MySQL中, GROUP BY 可以对结果进行排序,而且你可以在GROUP BY列表指定的列中使用明确的 ASC和DESC关键词,从而对个别列进行排序。 (不论如何排序被ROLLUP添加的较高级别的总计行仍出现在它们被计算出的行后面)。
LIMIT可用来限制返回客户端的行数。LIMIT 用在 ROLLUP后面, 因此这个限制 会取消被ROLLUP添加的行。例如:
mysql> SELECT year, country, product, SUM(profit) -> FROM sales -> GROUP BY year, country, product WITH ROLLUP -> LIMIT 5;+------+---------+------------+-------------+ | year | country | product | SUM(profit) | +------+---------+------------+-------------+ | 2000 | Finland | Computer | 1500 | | 2000 | Finland | Phone | 100 | | 2000 | Finland | NULL | 1600 | | 2000 | India | Calculator | 150 | | 2000 | India | Computer | 1200 | +------+---------+------------+-------------+
将ROLLUP同 LIMIT一起使用可能会产生更加难以解释的结果,原因是对于理解高聚集行,你所掌握的上下文较少。
在每个高聚集行中的NULL 指示符会在该行被送至客户端时产生。服务器会查看最左边的改变值后面的GROUP BY子句指定的列。对于任何结果集合中的,有一个词匹配这些名字的列, 其值被设为 NULL。(若你使用列数字指定了分组列,则服务器会通过数字确定将哪个列设置为 NULL)。
由于在高聚集行中的 NULL值在问询处理阶段被放入结果集合中,你无法将它们在问询本身中作为NULL值检验。例如,你无法将 HAVING product IS NULL 添加到问询中,从而在输出结果中删去除了高聚集行以外的部分。
另一方面, NULL值在客户端不以 NULL 的形式出现, 因而可以使用任何MySQL客户端编程接口进行检验。
3. 具有隐含字段的GROUP BY
MySQL 扩展了 GROUP BY的用途,因此你可以使用SELECT 列表中不出现在GROUP BY语句中的列或运算。这代表 “对该组的任何可能值 ”。你可以通过避免排序和对不必要项分组的办法得到它更好的性能。例如,在下列问询中,你无须对customer.name 进行分组:
mysql> SELECT order.custid, customer.name, MAX(payments) -> FROM order,customer -> WHERE order.custid = customer.custid -> GROUP BY order.custid;
在标准SQL中, 你必须将 customer.name添加到 GROUP BY子句中。在MySQL中, 假如你不在ANSI模式中运行,则这个名字就是多余的。
假如你从 GROUP BY 部分省略的列在该组中不是唯一的,那么不要使用这个功能! 你会得到非预测性结果。
在有些情况下,你可以使用MIN()和MAX() 获取一个特殊的列值,即使他不是唯一的。下面给出了来自包含排序列中最小值的列中的值:
SUBSTR(MIN(CONCAT(RPAD(sort,6,' '),column)),7)
注意,假如你正在尝试遵循标准 SQL, 你不能使用GROUP BY或 ORDER BY子句中的表达式。你可以通过使用表达式的别名绕过这一限制:
mysql> SELECT id,FLOOR(value/100) AS val -> FROM tbl_name -> GROUP BY id, val ORDER BY val;
然而, MySQL允许你使用GROUP BY 及 ORDER BY 子句中的表达式。例如:
mysql> SELECT id, FLOOR(value/100) FROM tbl_name ORDER BY RAND();