分组查询

Posted XQR

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了分组查询相关的知识,希望对你有一定的参考价值。

group by:通过某些字段进行分组

emp表:

+-------+--------+-----------+------+------------+---------+---------+--------+
| empno | ename  | job       | mgr  | hiredate   | sal     | comm    | deptno |
+-------+--------+-----------+------+------------+---------+---------+--------+
|  7369 | SMITH  | CLERK     | 7902 | 1980-12-17 |  800.00 | NULL    |     20 |
|  7499 | ALLEN  | SALESMAN  | 7698 | 1981-02-20 | 1600.00 |  300.00 |     30 |
|  7521 | WARD   | SALESMAN  | 7698 | 1981-02-22 | 1250.00 |  500.00 |     30 |
|  7566 | JONES  | MANAGER   | 7839 | 1987-04-02 | 2975.00 | NULL    |     20 |
|  7654 | MARTIN | SALESMAN  | 7698 | 1987-09-28 | 1250.00 | 1400.00 |     30 |
|  7698 | BLAKE  | MANAGER   | 7839 | 1987-05-01 | 2850.00 | NULL    |     30 |
|  7782 | CLARK  | MANAGER   | 7839 | 1981-06-09 | 2450.00 | NULL    |     10 |
|  7788 | SCOTT  | ANALYST   | 7566 | 1987-04-19 | 3000.00 | NULL    |     20 |
|  7839 | KING   | PRESIDENT | NULL | 1981-01-17 | 5000.00 | NULL    |     10 |
|  7844 | TURNER | SALESMAN  | 7698 | 1981-09-08 | 1500.00 |    0.00 |     30 |
|  7876 | ADAMS  | CLERK     | 7788 | 1987-05-23 | 1100.00 | NULL    |     20 |
|  7900 | JAMES  | CLERK     | 7698 | 1987-12-03 |  950.00 | NULL    |     30 |
|  7902 | FORD   | ANALYST   | 7566 | 1981-12-03 | 3000.00 | NULL    |     20 |
|  7934 | MILLER | CLERK     | 7782 | 1982-01-23 | 1300.00 | NULL    |     10 |
+-------+--------+-----------+------+------------+---------+---------+--------+

例1:找出每个工作岗位的最高薪水

mysql> select max(sal) from emp group by job;
+----------+
| max(sal) |
+----------+
|  3000.00 |
|  1300.00 |
|  2975.00 |
|  5000.00 |
|  1600.00 |
+----------+

 

***重点注意:

1.若一条DQL语句中有group by子句,那么select关键字后面只能跟参与分组的字段和分组函数

  在MySQL中,语法比较松散,可以把其他的字段编写进去,但是查询结果会报错;

  在Oracle中,语法比较严谨,如果输入其他子句会报错

2.group by后面可以跟多个字段,联合起来分组

  例2:找出不同部门中不同工作岗位的最高薪水

mysql> select deptno,job,max(sal) from emp group by deptno,job;
+--------+-----------+----------+
| deptno | job       | max(sal) |
+--------+-----------+----------+
|     10 | CLERK     |  1300.00 |
|     10 | MANAGER   |  2450.00 |
|     10 | PRESIDENT |  5000.00 |
|     20 | ANALYST   |  3000.00 |
|     20 | CLERK     |  1100.00 |
|     20 | MANAGER   |  2975.00 |
|     30 | CLERK     |   950.00 |
|     30 | MANAGER   |  2850.00 |
|     30 | SALESMAN  |  1600.00 |
+--------+-----------+----------+

3.where子句后面为什么不能出现分组函数?:因为where子句的功能是在group by之前完成数据过滤,还没分组不能调用分组函数

 

 

having:分组之后进行过滤,一般能在where过滤的尽量在where过滤,避免无效数据参与分组

例3:找出每个岗位的平均薪水,要求显示平均薪水大于1500

mysql> select job,avg(sal) from emp group by job having avg(sal)>1500;
+-----------+-------------+
| job       | avg(sal)    |
+-----------+-------------+
| ANALYST   | 3000.000000 |
| MANAGER   | 2758.333333 |
| PRESIDENT | 5000.000000 |
+-----------+-------------+

 

以上是关于分组查询的主要内容,如果未能解决你的问题,请参考以下文章

oracle分组查询

oracle分组查询

你如何在 python 中处理 graphql 查询和片段?

Microsoft SQL Server 代码片段收集

oracle 分组查询 子查询 统计查询 FROM加子查询临时表 查询高于平均工资 示例代码

Mysql 的按时间段分组查询