• MySQL GROUP BY和HAVING的使用 2022/09/09


    🔥group by应用场景🔥

    我们已经掌握使用select语句结合where查询条件获取需要的数据,但在实际的应用中,还会遇到下面这类需求,又该如何解决?

    1、公司想知道每个部门有多少员工

    2、班主任想统计各科成绩的第一名

    3、某门店想掌握男、女性会员的人数和平均年龄

    🔥group by的使用🔥

     从字面上理解,group by表示根据某种规则对数据进行分组,它必须配合聚合函数进行使用,对数据进行分组后可以进行count、sum、avg、max和min等运算。

    group by语法

    SELECT column_name,aggregate_function(column_name) FROM table_name GROUP BY column_name

    说明:

    1、aggregate_function 表示聚合函数

    2、group by 可以对一列或多列进行分组

    1. mysql> alter table employee add column dept varchar(20) comment '部门名称';
    2. Query OK, 0 rows affected (0.06 sec)
    3. Records: 0 Duplicates: 0 Warnings: 0
    4. mysql> desc employee;
    5. +--------+-------------+------+-----+---------+----------------+
    6. | Field | Type | Null | Key | Default | Extra |
    7. +--------+-------------+------+-----+---------+----------------+
    8. | id | int | NO | PRI | NULL | auto_increment |
    9. | name | varchar(30) | YES | | NULL | |
    10. | sex | varchar(1) | YES | | NULL | |
    11. | salary | int | YES | | NULL | |
    12. | dept | varchar(20) | YES | | NULL | |
    13. +--------+-------------+------+-----+---------+----------------+
    14. 5 rows in set (0.00 sec)
    15. mysql> update employee set dept='部门A' where id='1';
    16. Query OK, 1 row affected (0.01 sec)
    17. Rows matched: 1 Changed: 1 Warnings: 0
    18. mysql> update employee set dept='部门B' where id='2';
    19. Query OK, 1 row affected (0.01 sec)
    20. Rows matched: 1 Changed: 1 Warnings: 0
    21. mysql> update employee set dept='部门B' where id='3';
    22. Query OK, 1 row affected (0.01 sec)
    23. Rows matched: 1 Changed: 1 Warnings: 0
    24. mysql> select * from employee;
    25. +----+--------+------+--------+-------+
    26. | id | name | sex | salary | dept |
    27. +----+--------+------+--------+-------+
    28. | 1 | 张三 | 男 | 5500 | 部门A |
    29. | 2 | 李四 | 男 | 4500 | 部门B |
    30. | 3 | 张小妹 | 女 | 4500 | 部门B |
    31. +----+--------+------+--------+-------+
    32. 3 rows in set (0.00 sec)
    1. select sex,count(*) from employee group by sex;
    2. +------+----------+
    3. | sex | count(*) |
    4. +------+----------+
    5. | 男 | 2 |
    6. | 女 | 1 |
    7. +------+----------+
    8. 2 rows in set (0.00 sec)
    1. select dept,count(*) from employee group by dept;
    2. +-------+----------+
    3. | dept | count(*) |
    4. +-------+----------+
    5. | 部门A | 1 |
    6. | 部门B | 2 |
    7. +-------+----------+
    8. 2 rows in set (0.00 sec)
    1. select dept,sum(salary) from employee group by dept;
    2. +-------+-------------+
    3. | dept | sum(salary) |
    4. +-------+-------------+
    5. | 部门A | 5500 |
    6. | 部门B | 9000 |
    7. +-------+-------------+
    8. 2 rows in set (0.00 sec)
    1. select dept,sum(salary) from employee group by dept;
    2. +-------+-------------+
    3. | dept | sum(salary) |
    4. +-------+-------------+
    5. | 部门A | 5500 |
    6. | 部门B | 9000 |
    7. +-------+-------------+
    8. 2 rows in set (0.00 sec)
    1. select dept,min(salary) from employee group by dept;
    2. +-------+-------------+
    3. | dept | min(salary) |
    4. +-------+-------------+
    5. | 部门A | 5500 |
    6. | 部门B | 4500 |
    7. +-------+-------------+
    8. 2 rows in set (0.00 sec)

    🔥 having的使用 🔥

    在SQL中增加HAVING子句原因是,WHERE 关键字无法与聚合函数一起使用。HAVING子句可以对分组后的各数据进行筛选。

    having语法

    1. SELECT column_name,aggregate_function(column_name)
    2. FROM table_name WHERE column_name operator value
    3. GROUP BY column_name HAVING aggregate_function(column_name) operator value
    1. mysql> select dept, count(*) from employee group by dept;
    2. +-------+----------+
    3. | dept | count(*) |
    4. +-------+----------+
    5. | 部门A | 1 |
    6. | 部门B | 2 |
    7. +-------+----------+
    8. 2 rows in set (0.00 sec)
    9. mysql> select dept, count(*) from employee group by dept having count(*)<2;
    10. +-------+----------+
    11. | dept | count(*) |
    12. +-------+----------+
    13. | 部门A | 1 |
    14. +-------+----------+
    15. 1 row in set (0.00 sec)
    16. mysql> select dept, count(*) from employee group by dept having count(*)<3;
    17. +-------+----------+
    18. | dept | count(*) |
    19. +-------+----------+
    20. | 部门A | 1 |
    21. | 部门B | 2 |
    22. +-------+----------+
    23. 2 rows in set (0.00 sec)
    1. mysql> select dept, max(salary) from employee group by dept;
    2. +-------+-------------+
    3. | dept | max(salary) |
    4. +-------+-------------+
    5. | 部门A | 5500 |
    6. | 部门B | 4500 |
    7. +-------+-------------+
    8. 2 rows in set (0.00 sec)
    9. mysql> select dept,max(salary) from employee group by dept having max(salary)>=5000;
    10. +-------+-------------+
    11. | dept | max(salary) |
    12. +-------+-------------+
    13. | 部门A | 5500 |
    14. +-------+-------------+
    15. 1 row in set (0.00 sec)
  • 相关阅读:
    Mysql 视图
    Java面向对象程序设计综合练习4(编程题)
    实现Runnable接口
    【虚拟机】Vmware虚拟机桥接 Destination Host Unreachable 问题解决
    ardupilot 中电机输出逻辑及电机转轴状态分析
    ARM汇编指令之数据操作指令
    Java项目:springBoot+Mysql实现的校园二手在线交易平台系统
    力扣刷题 day50:10-20
    BigDecimal舍入模式总结及运用场景分析
    【Java面试】数据库连接池有什么用?它有哪些关键参数?
  • 原文地址:https://blog.csdn.net/u013491829/article/details/126781601