MySql聚合函数简单介绍
聚合函数只能出现在SELECT列表、HAVING子句和ORDER BY子句中,不能出现在WHERE子句中。
测试:
SELECT
u.id,
u.name,
o.id,
d.id,
d.`name`
FROM
`user` u
left JOIN `order` o ON u.id = o.user_id
left JOIN department d ON d.id = u.department_id
WHERE
max(u.id) < 24242424242432442
ORDER BY
u.name desc,
u.id desc;
结果:

count()
统计满足条件的记录数量
select count(*) from s
select count(distinct email) from s
统计表中email列有多少条有值的记录 消除重复值
Sum()
统计总数
select SUM(age) from s where email is not null
Avg()
求平均值
select avg(age) from s where email is not null
Max()
求最大值
Min()
求最小值
select avg(age),MAX(age),MIN(age) from s
group_concat()
将满足条件的记录,显示成一行,使用逗号分开
select group_concat(sname) from s where sid<5
相关推荐
-
MySQL 安装失败,提示Apply Security Settings 的处理办法
MySQL 安装失败,提示Apply Security Settings 的处理办法2025-04-20 01:54:57 -
MySQL事务隔离级别详解2025-04-20 01:44:01
-
一文说清nginx规则匹配(含案例分析)2025-04-20 01:10:02
-
运维服务篇:Nginx常用功能(rewrite重定向/location定位等)
运维服务篇:Nginx常用功能(rewrite重定向/location定位等)2025-04-20 00:55:25 -
php定义变量规则不能包含哪些字符?2025-04-20 00:27:24