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 中一切皆表

相关推荐
RestCloud10 小时前
SQL Server到Hive:批处理ETL性能提升30%的实战经验
数据库·api
RestCloud10 小时前
为什么说零代码 ETL 是未来趋势?
数据库·api
ClouGence12 小时前
CloudCanal + Paimon + SelectDB 从 0 到 1 构建实时湖仓
数据库
Java水解14 小时前
Mysql查看执行计划、explain关键字详解(超详细)
后端·mysql
知其然亦知其所以然18 小时前
MySQL 社招必考题:如何优化查询过程中的数据访问?
后端·mysql·面试
DemonAvenger19 小时前
NoSQL与MySQL混合架构设计:从入门到实战的最佳实践
数据库·mysql·性能优化
程序新视界20 小时前
如何在MySQL中创建聚集索引?
mysql
AAA修煤气灶刘哥1 天前
后端人速藏!数据库PD建模避坑指南
数据库·后端·mysql
程序新视界1 天前
学习MySQL绕不开的两个基础概念:聚集索引与非聚集索引
mysql
RestCloud1 天前
跨境数据传输:ETL如何处理时区与日期格式差异
mysql·api