SQL 中 HAVING 用法
现在 Student表有 如下数据
现需求如下: 查找每个老师的学生的平均年龄且平均年齿大于12
不使用 HAVING
1 SELECT * FROM (SELECT TeacherID, AVG(Age) AS AGE FROM Student 2 GROUP BY TeacherID) T WHERE T.AGE > 12
使用HAVING
1 SELECT TeacherID, AVG(Age) AS AGE FROM Student 2 GROUP BY TeacherID 3 HAVING AVG(Age) > 12
结果如下:
HAVING:给分组设置条件