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
View Code

使用HAVING

1   SELECT TeacherID, AVG(Age) AS AGE FROM Student
2   GROUP BY TeacherID
3   HAVING AVG(Age) > 12
View Code

结果如下:

 

HAVING:给分组设置条件

 

posted @ 2017-02-14 17:29  老糖  Views(36242)  Comments(0Edit  收藏  举报