🌈 个人主页:
🔥 系列专栏:《mysql》
💪🏻 制定明确可量化的目标,坚持默默的做事。
✨欢迎加入探索mysql聚合函数之旅✨
👋 大家好!文本学习和探索mysql聚合函数。👋 效率与精准是制胜的关键。mysql聚合函数,作为数据库操作中的强大工具,能帮你快速进行数据统计、分析和汇总。本篇文章将带你轻松上手mysql聚合函数,让复杂的数据处理变得简单高效。准备好提升你的数据库技能了吗?让我们一同揭开mysql聚合函数的神秘面纱,让你的数据飞起来!
目录
十一、json_objectagg 聚合为一个 json 对象
一、avg 求平均值
mysql avg()
函数计算并返回表达式的平均值。
忽略 null
值。
语法:
avg(expr)
avg(distinct expr)
select avg(expr), ...
from table_name
[where ...];
select avg(expr), group_expr1, group_expr2, ...
from table_name
[where ...]
group by group_expr1, group_expr2, ...;
参数说明
-
expr:
expr
一个用于聚合运算的表达式。它可以是一个列名,也可以是一个表达式 -
group_expr1, group_expr2, ...
:用于分组的表达式或者列名。
返回值
- 表达式的平均值。
- 返回
null情况:
没有匹配的行。
示例:
create table `student_score` (
`score` int not null
);
insert into `student_score` (`score`)
values (80),
(90),
(84),
(96),
(80),
( 98),
( 75);
select avg(score) from student_score; // 86.1429
二、
count 统计数量(非null值)
mysql count()
函数用于统计表达式代表的所有值的中的非 null 值的数量。
语法:
count(expr)
select count(expr), ...
from table_name
[where ...];
select count(expr), group_expr1, group_expr2, ...
from table_name
[where ...]
group by group_expr1, group_expr2, ...;
参数说明
-
expr:
expr
一个需要统计数量的表达式。它可以是一个列名,也可以是一个表达式。 -
group_expr1, group_expr2, ...
:用于分组的表达式或者列名。
返回值
- 所有行中的表达式代表的所有值的中的非
null
值的数量。 - 注意: 如果使用
count(*)
或者类似于count(1)
之类的语句,则会统计null
值。
示例:
create table `student_score` (
`score` int
);
insert into `student_score` (`score`)
values (80),
(90),
(84),
(96),
(80),
( 98),
( 75),
( null);
select
count(score), // 7
count(*), // 8
count(1) // 8
from student_score;
三、group_concat 分组列连接成字符串
mysql group_concat()
函数将一个分组中指定的列或表达式的值连接成一个字符串并返回。
语法:
group_concat(
[distinct] expr [, expr2 ...]
[order by ...]
[separator separator]
)
select group_concat(expr), ...
from table_name
[where ...]
[group by group_expr1, group_expr2, ...];
参数说明
-
expr [, expr2 ...]
:必须的。它指定了要连接的一个或者多个列或表达式。 -
order by
:可选的。它用于对要连接的内容排序。 -
separator separator
:可选的。separator
连接符。默认是,
。
返回值
- 将通过列或者表达式指定的内容连接起来。
- 返回
null情况:
结果集没有任何行。
示例:
drop table if exists `student_score`;
create table `student_score` (
`id` int primary key auto_increment,
`name` varchar(255) not null,
`score` int not null
);
select
name as `name`,
group_concat(subject) as `subjects`
from student_score
group by name;
+------+--------------+
| name | subjects |
+------+--------------+
| tim | english |
| tom | math,english |
+------+--------------+
----------------------------------------------------------
select
name as `name`,
group_concat(subject separator '/') as `subjects`
from student_score
group by name;
+------+--------------+
| name | subjects |
+------+--------------+
| tim | english |
| tom | math/english |
+------+--------------+
----------------------------------------------------------
select
name as `name`,
group_concat(
concat(subject, '-'), score
order by subject
separator '/'
) as `scores`
from student_score
group by name;
或
select
name as `name`,
group_concat(
concat_ws('-', subject, score)
order by subject
separator '/'
) as `scores`
from student_score
group by name;
+------+--------------------+
| name | scores |
+------+--------------------+
| tim | english-98 |
| tom | english-90/math-80 |
+------+--------------------+
四、max 取最大值
mysql max() 函数返回表达式代表的所有值中的最大值。如果您需要获取一个表达式中的最小值,请使用 min() 函数。
语法:
max(expr)
select max(expr), ...
from table_name
[where ...];
select max(expr), group_expr1, group_expr2, ...
from table_name
[where ...]
group by group_expr1, group_expr2, ...;
参数说明
-
expr:
expr
一个用于聚合运算的表达式。它可以是一个列名,也可以是一个表达式 -
group_expr1, group_expr2, ...
:用于分组的表达式或者列名。
返回值
- 表达式代表的所有值中的最大值。
- 返回
null情况:
没有匹配的行。
示例:
select max(score) from student_score; // 98
select name, max(score) from student_score group by name;
+------+------------+
| name | max(score) |
+------+------------+
| tom | 90 |
| tim | 98 |
+------+------------+
五、min 取最小值
mysql min() 函数返回表达式代表的所有值中的最小值。如果您需要获取一个表达式中的最大值,请使用 max() 函数。
语法:
min(expr)
select min(expr), ...
from table_name
[where ...];
select min(expr), group_expr1, group_expr2, ...
from table_name
[where ...]
group by group_expr1, group_expr2, ...;
参数说明
-
expr:
expr
一个用于聚合运算的表达式。它可以是一个列名,也可以是一个表达式。 -
group_expr1, group_expr2, ...
:用于分组的表达式或者列名。
返回值
- 表达式代表的所有值中的最小值。
- 返回
null情况:
没有匹配的行。
示例:
mysql> select min(score) from student_score;
+------------+
| min(score) |
+------------+
| 80 |
+------------+
mysql> select subject, min(score) from student_score group by subject;
+---------+------------+
| subject | min(score) |
+---------+------------+
| math | 80 |
| english | 90 |
+---------+------------+
六、sum 求和(非null值)
mysql sum()
函数计算所有指定的非 null 值的总和并返回。
语法:
sum(expr)
select sum(expr), ...
from table_name
[where ...];
select sum(expr), group_expr1, group_expr2, ...
from table_name
[where ...]
group by group_expr1, group_expr2, ...;
参数说明
-
expr:
expr
一个需要统计数量的表达式。它可以是一个列名,也可以是一个表达式。 -
group_expr1, group_expr2, ...
:用于分组的表达式或者列名。
返回值
- 所有指定的非 null 值的总和。
- 返回
null情况:没有匹配的行。
示例:
mysql> select sum(score) from student_score;
+------------+
| sum(score) |
+------------+
| 268 |
+------------+
mysql> select name, sum(score)from student_score group by name;
+------+------------+
| name | sum(score) |
+------+------------+
| tom | 170 |
| tim | 98 |
+------+------------+
七、bit_and 按位与运算(非null值)
mysql bit_and() 函数是一个聚合函数,它对所有的非 null 输入值执行"按位与"运算。(只处理那些非 null 的值)
按位与处理两个长度相同的二进制数,两个相应的二进位都为 1,该位的结果值才为 1,否则为 0。
语法:
bit_and(expr)
select bit_and(expr), ...
from table_name
[where ...];
select bit_and(expr), group_expr1, group_expr2, ...
from table_name
[where ...]
group by group_expr1, group_expr2, ...;
参数说明
-
expr
:必需的。一个列名或者表达式。它接受一个数值或者二进制值。
返回值
- 返回值的类型与输入参数的类型相同,它返回 对所有的非 null 输入值执行"按位与"运算的结果。
- 返回
null情况:
所有的输入的值为 null。
示例:
mysql> select bit_and(x) from (select 4 x union select 5 x union select 6 x ) t;
+------------+
| bit_and(x) |
+------------+
| 4 |
+------------+
“按位与” 运算,运算步骤如下:
4 -> 100
5 -> 101
6 -> 110
bit_and() = 100 = 4
八、bit_or 按位或运算(非null值)
mysql bit_or() 函数是一个聚合函数,它对所有的非 null 输入值执行"按位或"运算。(只处理那些非 null 的值)
按位或处理两个长度相同的二进制数,两个相应的二进位都为 0,该位的结果值为 0,否则为 1。
语法:
bit_or(expr)
select bit_or(expr), ...
from table_name
[where ...];
select bit_or(expr), group_expr1, group_expr2, ...
from table_name
[where ...]
group by group_expr1, group_expr2, ...;
参数说明
-
expr
:必需的。一个列名或者表达式。它接受一个整数或者 bit 类型的值。
返回值
- 返回值的类型与输入参数的类型相同,它返回 对所有的非 null 输入值执行"按位或"运算的结果。
- 返回
null情况:
所有的输入的值为 null。
示例:
mysql> select bit_or(x) from (select 4 x union select 5 x union select 6 x ) t;
+-----------+
| bit_or(x) |
+-----------+
| 7 |
+-----------+
“按位与” 运算,运算步骤如下:
4 -> 100
5 -> 101
6 -> 110
bit_or() = 111 = 7
九、bit_xor 按位异或运算(非null值)
mysql bit_xor() 函数是一个聚合函数,它对所有的非 null 输入值执行"按位异或"运算。(只处理那些非 null 的值)
按位异或处理两个长度相同的二进制数,两个相应的二进位只要不同,该位的结果值为 1,否则为 0。
语法:
bit_xor(expr)
select bit_xor(expr), ...
from table_name
[where ...];
select bit_xor(expr), group_expr1, group_expr2, ...
from table_name
[where ...]
group by group_expr1, group_expr2, ...;
参数说明
-
expr
:必需的。一个列名或者表达式。它接受一个整数或者 bit 类型的值。
返回值
- 返回值的类型与输入参数的类型相同,它返回 对所有的非 null 输入值执行"按位异或"运算的结果。
- 返回
null情况:
所有的输入的值为 null。
示例:
mysql> select bit_xor(x) from ( select 4 x union select 5 x union select 6 x ) t;
+------------+
| bit_xor(x) |
+------------+
| 7 |
+------------+
“按位异或” 运算,运算步骤如下:
4 -> 100
5 -> 101
6 -> 110
bit_xor() = 111 = 7
十、json_arrayagg 聚合为一个 json 数组
mysql json_arrayagg()
函数将指定的列或者表达式的值聚合为一个 json 数组。
语法:
json_arrayagg(expr)
select json_arrayagg(expr), ...
from table_name
[where ...]
[group by group_expr1, group_expr2, ...];
参数说明
-
expr
:必须的。它可以是一个列名,也可以是一个表达式。
返回值
- 聚合了所有符合条件的值。
- 返回
null情况:
结果集没有任何行。
示例:
mysql> select * from student_score;
+----+------+---------+-------+
| id | name | subject | score |
+----+------+---------+-------+
| 1 | tom | math | 80 |
| 2 | tom | english | 90 |
| 3 | tim | english | 98 |
+----+------+---------+-------+
3 rows in set (0.00 sec)
mysql> select name as `name`, json_arrayagg(subject) as `subjects` from student_score group by name;
+------+---------------------+
| name | subjects |
+------+---------------------+
| tim | ["english"] |
| tom | ["math", "english"] |
+------+---------------------+
十一、json_objectagg 聚合为一个 json 对象
mysql json_objectagg()
函数将由第一个参数作为键和第二个参数作为值的键值对聚合为一个 json 对象。
语法:
json_objectagg(key_expr, value_expr)
select json_objectagg(key_expr, value_expr), ...
from table_name
[where ...]
[group by group_expr1, group_expr2, ...];
参数说明
-
key_expr
:必须的。它的值作为结果对象中的键值对中的键。它可以是一个列名,也可以是一个表达式。 -
value_expr
:可选的。它的值作为结果对象中的键值对中的值。它可以是一个列名,也可以是一个表达式。
返回值
- 一个 json 对象,其中的键值对中的键是
key_expr
的值,值是value_expr
的值。 - 返回
null情况:
结果集没有任何行。 - 注意:如果存在重复的键,则只保留最后一个键作为键值对,其他重复的键值对都被丢弃。
示例:
mysql> select * from student_score;
+----+------+---------+-------+
| id | name | subject | score |
+----+------+---------+-------+
| 1 | tom | math | 80 |
| 2 | tom | english | 90 |
| 3 | tim | english | 98 |
+----+------+---------+-------+
3 rows in set (0.00 sec)
mysql> select name as `name`, json_objectagg(subject, score) as `scores`from student_score group by name;
+------+-----------------------------+
| name | scores |
+------+-----------------------------+
| tim | {"english": 98} |
| tom | {"math": 80, "english": 90} |
+------+-----------------------------+
十二、std 求总体标准差(非null)
mysql std()
函数计算所有非 null 输入值的总体标准差并返回结果。 它是 stddev_pop() 的别名。
只处理那些非 null 的值,忽略null值。
语法:
std(expr)
参数说明
-
expr:
必需的。一个列名或者表达式。它接受一个数值或者二进制值。
返回值
- 总体标准差。
- 返回
null情况:
所有的输入的值为 null。
示例:
mysql> select std(x) from ( select 4 x union select 5 x union select 6 x) t;
+-------------------+
| std(x) |
+-------------------+
| 0.816496580927726 |
+-------------------+
十三、stddev 求总体标准差(非null)
mysql stddev() 函数计算所有非 null 输入值的总体标准差并返回结果。 它是 stddev_pop() 的别名。
只处理那些非 null 的值,null 值会被忽略。
语法:
stddev(expr)
select avg(expr), ...
from table_name
[where ...];
select avg(expr), group_expr1, group_expr2, ...
from table_name
[where ...]
group by group_expr1, group_expr2, ...;
参数说明
-
expr
:必需的。一个列名或者表达式。它接受一个数值或者二进制值。
返回值
- 所有非 null 输入值的总体标准差。
- 返回
null情况:
所有的输入的值为 null。
示例:
mysql> select stddev(x) from ( select 4 x union select 5 x union select 6 x) t;
+-------------------+
| stddev(x) |
+-------------------+
| 0.816496580927726 |
+-------------------+
十四、stddev_pop 求总体标准差(非null)
mysql stddev_pop()
函数计算所有非 null 输入值的总体标准差并返回结果。
只处理那些非 null 的值,null 值会被函数忽略。
语法:
stddev_pop(expr)
select stddev_pop(expr), ...
from table_name
[where ...];
select stddev_pop(expr), group_expr1, group_expr2, ...
from table_name
[where ...]
group by group_expr1, group_expr2, ...;
参数说明
-
expr
:必需的。一个列名或者表达式。它接受一个数值或者二进制值。
返回值
- 所有非 null 输入值的总体标准差。
- 返回
null情况:
所有的输入的值为 null。
示例:
mysql> select stddev_pop(x) from ( select 4 x union select 5 x union select 6 x) t;
+-------------------+
| stddev_pop(x) |
+-------------------+
| 0.816496580927726 |
+-------------------+
十五、stddev_samp 计算样本标准差(非null)
mysql stddev_samp()
函数计算所有非 null 输入值的样本标准差并返回结果。
只处理那些非 null 的值,null 值会被函数忽略。
语法:
stddev_samp(expr)
select stddev_samp(expr), ...
from table_name
[where ...];
select stddev_samp(expr), group_expr1, group_expr2, ...
from table_name
[where ...]
group by group_expr1, group_expr2, ...;
参数说明
-
expr
:必需的。一个列名或者表达式。它接受一个数值或者二进制值。
返回值
- 所有非 null 输入值的样本标准差。
- 返回
null情况:
所有的输入的值为 null。
示例:
mysql> select stddev_samp(x) from ( select 4 x union select 5 x union select 6 x) t;
+----------------+
| stddev_samp(x) |
+----------------+
| 1 |
+----------------+
十六、var_pop 计算总体方差(非null)
mysql var_pop()
函数计算所有非 null 输入值的总体方差(总体标准差的平方)并返回结果。
只处理那些非 null 的值,null 值会被函数忽略。
语法:
var_pop(expr)
select var_pop(expr), ...
from table_name
[where ...];
select var_pop(expr), group_expr1, group_expr2, ...
from table_name
[where ...]
group by group_expr1, group_expr2, ...;
参数说明
-
expr
:必需的。一个列名或者表达式。它接受一个数值或者二进制值。
返回值
- 所有非 null 输入值的总体方差(总体标准差的平方)。
- 返回
null情况:
所有的输入的值为 null。
示例:
select var_pop(x)
from (
select 11 x
union
select 12 x
union
select 13 x
) t;
-- 输出
+--------------------+
| var_pop(x) |
+--------------------+
| 0.6666666666666666 |
+--------------------+
十七、variance 计算总体方差(非null)
mysql var_samp()
函数计算所有非 null 输入值的样本方差(样本标准差的平方)并返回结果。
只处理那些非 null 的值,null 值会被函数忽略。
语法:
var_samp(expr)
select var_samp(expr), ...
from table_name
[where ...];
select var_samp(expr), group_expr1, group_expr2, ...
from table_name
[where ...]
group by group_expr1, group_expr2, ...;
参数说明
-
expr
:必需的。一个列名或者表达式。它接受一个数值或者二进制值。
返回值
- 所有非 null 输入值的样本方差(样本标准差的平方)。
- 返回
null情况:
所有的输入的值为 null。
示例:
mysql> select var_samp(x) from ( select 4 x union select 5 x union select 6 x) t;
+-------------+
| var_samp(x) |
+-------------+
| 1 |
+-------------+
十八、var_samp 计算样本方差(非null)
mysql variance() 函数计算所有非 null 输入值的总体方差(总体标准差的平方)并返回结果。 它是 var_pop() 的别名。
只处理那些非 null 的值,null 值会被函数忽略。
语法:
variance(expr)
select variance(expr), ...
from table_name
[where ...];
select variance(expr), group_expr1, group_expr2, ...
from table_name
[where ...]
group by group_expr1, group_expr2, ...;
参数说明
-
expr
:必需的。一个列名或者表达式。它接受一个数值或者二进制值。
返回值
-
返回所有非 null 输入值的总体方差(总体标准差的平方)。
-
返回
null情况:
所有的输入的值为 null。
示例:
mysql> select variance(x) from ( select 4 x union select 5 x union select 6 x) t;
+--------------------+
| variance(x) |
+--------------------+
| 0.6666666666666666 |
+--------------------+
好了,今天分享到这里。希望你喜欢这次的探索之旅!不要忘记 "点赞" 和 "关注" 哦,我们下次见!🎈
发表评论