12.20.1 Aggregate Function Descriptions聚合函数描述

This section describes aggregate functions that operate on sets of values. 本节介绍对值集进行操作的聚合函数。They are often used with a GROUP BY clause to group values into subsets.它们通常与GROUP BY子句一起用于将值分组到子集中。

Table 12.25 Aggregate Functions聚合函数

Name名称Description描述
AVG()Return the average value of the argument返回参数的平均值
BIT_AND()Return bitwise AND按位与返回
BIT_OR()Return bitwise OR按位或返回
BIT_XOR()Return bitwise XOR返回按位异或
COUNT()Return a count of the number of rows returned返回返回的行数的计数
COUNT(DISTINCT)Return the count of a number of different values返回多个不同值的计数
GROUP_CONCAT()Return a concatenated string返回串联字符串
JSON_ARRAYAGG()Return result set as a single JSON array将结果集作为单个JSON数组返回
JSON_OBJECTAGG()Return result set as a single JSON object作为单个JSON对象返回结果集
MAX()Return the maximum value返回最大值
MIN()Return the minimum value返回最小值
STD()Return the population standard deviation返回总体标准差
STDDEV()Return the population standard deviation返回总体标准差
STDDEV_POP()Return the population standard deviation返回总体标准差
STDDEV_SAMP()Return the sample standard deviation返回样本标准差
SUM()Return the sum返回总和
VAR_POP()Return the population standard variance返回总体标准方差
VAR_SAMP()Return the sample variance返回样本方差
VARIANCE()Return the population standard variance返回总体标准方差

Unless otherwise stated, aggregate functions ignore NULL values.除非另有说明,否则聚合函数忽略NULL值。

If you use an aggregate function in a statement containing no GROUP BY clause, it is equivalent to grouping on all rows. 如果在不包含GROUP BY子句的语句中使用聚合函数,则相当于对所有行进行分组。For more information, see Section 12.20.3, “MySQL Handling of GROUP BY”.有关更多信息,请参阅第12.20.3节,“GROUP BY的MySQL处理”

Most aggregate functions can be used as window functions. 大多数聚合函数都可以用作窗口函数。Those that can be used this way are signified in their syntax description by [over_clause], representing an optional OVER clause. 可以用这种方法的那些在语法描述中用[over_clause]表示,表示可选的OVER子句。over_clause is described in Section 12.21.2, “Window Function Concepts and Syntax”, which also includes other information about window function usage.over_clause第12.21.2节,“窗口函数概念和语法”中进行了描述,其中还包括有关窗口函数用法的其他信息。

For numeric arguments, the variance and standard deviation functions return a DOUBLE value. 对于数值参数,方差和标准差函数返回一个DOUBLE值。The SUM() and AVG() functions return a DECIMAL value for exact-value arguments (integer or DECIMAL), and a DOUBLE value for approximate-value arguments (FLOAT or DOUBLE).SUM()AVG()函数为精确值参数(INTEGERDECIMAL)返回一个DECIMAL值,为近似值参数(FLOATDOUBLE)返回一个DOUBLE值。

The SUM() and AVG() aggregate functions do not work with temporal values. SUM()AVG()聚合函数不能处理时态值。(They convert the values to numbers, losing everything after the first nonnumeric character.) (它们将值转换为数字,丢失第一个非数字字符之后的所有内容。)To work around this problem, convert to numeric units, perform the aggregate operation, and convert back to a temporal value. 要解决此问题,请转换为数字单位,执行聚合操作,然后转换回时间值。Examples:示例:

SELECT SEC_TO_TIME(SUM(TIME_TO_SEC(time_col))) FROM tbl_name;
SELECT FROM_DAYS(SUM(TO_DAYS(date_col))) FROM tbl_name;

Functions such as SUM() or AVG() that expect a numeric argument cast the argument to a number if necessary. 如果需要,SUM()AVG()等需要数字参数的函数会将参数转换为数字。For SET or ENUM values, the cast operation causes the underlying numeric value to be used.对于SETENUM值,强制转换操作将使用基础数值。

The BIT_AND(), BIT_OR(), and BIT_XOR() aggregate functions perform bit operations. BIT_AND()BIT_OR()BIT_XOR()聚合函数执行位操作。Prior to MySQL 8.0, bit functions and operators required BIGINT (64-bit integer) arguments and returned BIGINT values, so they had a maximum range of 64 bits. 在MySQL8.0之前,位函数和运算符需要BIGINT(64位整数)参数并返回BIGINT值,因此它们的最大范围为64位。Non-BIGINT arguments were converted to BIGINT prior to performing the operation and truncation could occur.BIGINT参数在执行操作之前已转换为BIGINT,可能会发生截断。

In MySQL 8.0, bit functions and operators permit binary string type arguments (BINARY, VARBINARY, and the BLOB types) and return a value of like type, which enables them to take arguments and produce return values larger than 64 bits. 在MySQL 8.0中,位函数和运算符允许二进制字符串类型的参数(BINARYVARBINARYBLOB类型)并返回类似类型的值,这使它们能够获取参数并生成大于64位的返回值。For discussion about argument evaluation and result types for bit operations, see the introductory discussion in Section 12.13, “Bit Functions and Operators”.有关位运算的参数计算和结果类型的讨论,请参阅第12.13节,“位函数和运算符”中的介绍性讨论。