当前位置:网站首页>分组查询以及筛选的区别
分组查询以及筛选的区别
2022-07-20 19:06:00 【Camellia——】
-- 分组查询:(group by)
-- 注意事项 查询字段:分组的字段,聚合函数
-- 分组查询加入where条件,必须先执行where条件,再去分组,如果满足条件才能分组
-- 需求:按照性别分组,分别求出男,女的数学平均分SELECT
sex,
AVG(math) '数学成绩平均分'
FROM
student
GROUP BY sex ;-- 需求:按照性别分组,分别求出男女的数学平均分以及总人数
SELECT
sex '性别', -- 分组字段
AVG(math) '数学平均分', -- 数学平均分
COUNT(id) '人数'
FROM
student
GROUP BY sex ;-- 需求:按照性别分组,分别求出男,女的数学平均分以及总人数 条件:数学成绩不大于70分的人不参与分组
SELECT
sex '性别',
AVG(math) '数学平均分',
COUNT(id) '人数'
FROM
student
WHERE math > 70 -- 条件判断
GROUP BY sex ; -- 分组-- 筛选(having):先进行分组,然后才筛选,而且having后可以跟聚合函数,ground by不能使用聚合函数
-- 需求需求:按照性别分组,分别求出男,女的数学平均分以及总人数 条件1):数学成绩不大于70分的人不参与分组
-- 条件2):总人数大于2个的人
SELECT
sex,
AVG(math),
COUNT(id)
FROM
student
WHERE math > 70
GROUP BY sex
HAVING COUNT(id) > 2 ;-- 简化
SELECT
sex '性别',
AVG(math) '数学平均分',
COUNT(id) '人数'
FROM
student
WHERE math > 70
GROUP BY sex
HAVING '人数' > 2 ;/*
having 和 group by的区别?
group by之前执行where语句,后面才是分组,having 放在group by之后
group by后面跟的分组字段,不能使用聚合函数;而having后面是可以使用聚合函数的!
*/
边栏推荐
- Starfish OS:以现实为纽带,打造元宇宙新范式
- 【JZOF】06从尾到头打印链表
- Selenium测试页面内容下载功能
- 话实践,行实干,成实事:“巡礼”数字化的中国大地
- The source code of the live broadcast system is automatically filled in when logging in
- 直播系统源码,登录时自动填取获得的验证码
- 7.5二叉搜索(排序)树
- Is it safe and reliable to open an account on the flush?
- How can we make enterprise knowledge management play its real value?
- 东风柳汽:创新引领高质量发展 用过硬产品赢得市场
猜你喜欢
5款经典代码阅读器的使用方案对比
CAD网页中实现嵌套打印
Ireport export PDF font bold failure
Use cpolar to build a business website (2)
How to realize the typewriter effect of text appearing one by one
【WMCA】《Biometric Face Presentation Attack Detection with Multi-Channel Convolutional Neural Network》
IReport导出PDF字体加粗失效
【Homography Estimation】《Deep Image Homography Estimation》
云计算与数字化转型的关系,终于有人讲明白了
[Estimation de la page d'accueil] estimation de la page d'accueil de l'image profonde
随机推荐
如何实现随叫随到的客户服务
[Estimation de la page d'accueil] estimation de la page d'accueil de l'image profonde
How to build a knowledge base web page?
【WMCA】《Biometric Face Presentation Attack Detection with Multi-Channel Convolutional Neural Network》
All countries know the highest salary level of programmers, and the bottom is unexpected
7.5 binary search (sort) tree
EasyNVS定制项目中的播放器更新及相应新功能增加
【WebFace260M】《WebFace260M:A Benchmark Unveiling the Power of Million-Scale Deep Face Recognition》
從頭開始實現 Promise
信息系统项目管理师---第十章 项目沟通管理和项目干系人管理
Former technical director of CEC talked about high-frequency interview questions: talk about your understanding of load balancing
Detailed explanation of zero length array in C language (2)
想开户炒美原油,但是总是担心不安全怎么办?
How to use DTS data subscription to obtain incremental data of DTS cache module?
【JZOF】06从尾到头打印链表
高并发下如何防重?
从头开始实现 Promise
IReport导出PDF字体加粗失效
SAFe Day 2022 中国规模化敏捷大会将于11月5日召开
【Homography Estimation】《Deep Image Homography Estimation》