having的用法

having子句限制的是组,不是行

栗子1:

查找订单总额少于2000的客户

SELECT Customer,SUM(BuysPrice) FROM Buys

GROUP BY Customer

HAVING SUM(BuysPrice) < 3000

栗子2:

数据库中有一张学生表student,有两个字段学生student_id,student_name

查询存在重复的student_id?

SELECT student_id FROM student WHERE student_id IN (SELECT student_id FROM student GROUP BY student_id HAVING COUNT(student_id) > 1)

在SQL中WHERE不能用于限制聚合函数,而HAVING可以用来限制聚合函数

原文地址:https://www.cnblogs.com/simpledu/p/14326084.html