mysql having 子句
HAVING 子句用来筛选分组后的各组数据。
一、HAVING 子句语法
SELECT 列名, 聚合函数(列名) FROM 表名 WHERE 条件表达式 GROUP BY 列表 HAVING 聚合函数(列名) operator value;
二、数据库实例
数据库有如下 score 表:

数据库还有如下 student 表:

一)不加 WHERE 子句
统计总成绩大于 200分的学生记录:
SELECT student_id,sum(score.score) as total_score FROM score INNER JOIN student ON score.student_id=student.ID GROUP BY student_id HAVING sum(score.score)> 200;
执行以上 SQL 语句,可得到如下结果集:

二)加 WHERE 子句
统计总成绩大于 200分、并且国籍是中国的学生记录:
SELECT student_id,sum(score.score) as total_score FROM score INNER JOIN student ON score.student_id=student.ID WHERE score.country = 'CN'
执行以上 SQL 语句,可得到如下结果集:

相关推荐
-
MySQL 安装失败,提示Apply Security Settings 的处理办法
MySQL 安装失败,提示Apply Security Settings 的处理办法2025-04-20 01:54:57 -
MySQL事务隔离级别详解2025-04-20 01:44:01
-
一文说清nginx规则匹配(含案例分析)2025-04-20 01:10:02
-
运维服务篇:Nginx常用功能(rewrite重定向/location定位等)
运维服务篇:Nginx常用功能(rewrite重定向/location定位等)2025-04-20 00:55:25 -
php定义变量规则不能包含哪些字符?2025-04-20 00:27:24