MySQL聚合统计

【数据库】MySQL聚合统计

王笃笃-CSDN博客https://blog.csdn.net/wangduduniubi?type=blog显示平均工资低于2000的部门和它的平均工资

mysql> select deptno,avg(sal) deptavg from emp group by deptno;

+--------+-------------+

| deptno | deptavg |

+--------+-------------+

| 20 | 2175.000000 |

| 30 | 1566.666667 |

| 10 | 2916.666667 |

+--------+-------------+

3 rows in set (0.01 sec)

mysql> select deptno,avg(sal) deptavg from emp group by deptno havingdeptavg<2000;

+--------+-------------+

| deptno | deptavg |

+--------+-------------+

| 30 | 1566.666667 |

+--------+-------------+

having对聚合后的数据进行条件筛选。

having 和 where 的区别

条件筛选的阶段不同

mysql> select deptno,job,avg(sal) myavg from emp where ename != 'SMITH' group by deptno,job having myavg<2000;

+--------+----------+-------------+

| deptno | job | myavg |

+--------+----------+-------------+

| 30 | SALESMAN | 1400.000000 |

| 20 | CLERK | 1100.000000 |

| 30 | CLERK | 950.000000 |

| 10 | CLERK | 1300.000000 |

+--------+----------+-------------+

where

对具体的任意列进行条件筛选

having

对分组聚合之后的结果进行条件筛选。

条件筛选的阶段不同

1

from emp

2

where ename != 'SMITH'

3

group by deptno,job

4

deptno,job,avg(sal) myavg

5

having myavg<2000;

中间筛选出来的和最终结果------>全部可以看做是逻辑上的表-->mysql 中一切皆表

相关推荐
杨_晨16 分钟前
LLM输出康熙部首冲突
数据库·python·mysql·ai
爱看报的猿20 分钟前
【金仓数据库征文】MySQL至金仓KES异构数据库平滑迁移与性能深度调优实战
数据库·数据仓库·mysql·金仓数据库征文
程序猿的学习34 分钟前
同一个 Key 写入两次,StarRocks 会留下什么?四种表模型讲明白了
数据库
朱峥嵘(朱髯)1 小时前
数据库如何根据全表 NDV 估算子集的 NDV
数据库·算法
ERD Online1 小时前
MySQL/Oracle/PG/SQLServer 存量库一键逆向成关系图
mysql·oracle·sqlserver
Nturmoils1 小时前
MongoDB迁移:从烟囱式部署走向 KES-AI 时代融合数据库架构
数据库
开开心心就好1 小时前
免费压缩软件WinRAR无弹窗、广告干扰
java·开发语言·前端·数据库·python·excel·winrar
深念Y1 小时前
hotspot-adb-research
linux·数据库·adb·emmc·随身wifi·移动终端
ERD Online1 小时前
我们怎么设计 good first issue:让第一个 PR 两小时内合入
数据库·git·后端·开源·issue
NeilYuen2 小时前
Mysql实战——图片读写
数据库·mysql