-
格式
-- 备注
select 展示项目名/*
from 表格名
where 查询条件
group by 分组项目
order by 排序项目及升降
having 聚合函数查询条件;
列:
COUNT:统计
-- 不同学历的各自人数
SELECT COUNT(education),education
FROM gy_user
GROUP BY education;
AVG:平均
-- 求每一个班级的平均年龄
SELECT AVG(age),class_type
FROM gy_user
GROUP BY class_type;
SUM:求和
-- 各班中年龄年龄总和大于80
SELECT class_type,SUM(age)
FROM gy_user
GROUP BY class_type
HAVING SUM(age)>80;
MAX:最大
-- 求每一个学历的最大年龄
SELECT MAX(age),education
FROM gy_user
GROUP BY education;
MIN:最小
-- 查询不同学历 最小年龄 小于30的学历是
SELECT MIN(age),education
FROM gy_user
GROUP BY education
HAVING MIN(age)<30;
GROUP BY:分组
-- 各个班级中平均年龄大于20的
SELECT AVG(age),class_type
FROM gy_user
GROUP BY class_type
HAVING AVG(age)>20;
HAVING:对聚合函数过滤
-- 各班最小年龄大于2
SELECT class_type,MIN(age)
FROM gy_user
GROUP BY class_type
HAVING MIN(age)>2;
网友评论