我想编写一个SQL查询,该查询将列出班级中有三(3)名以上学生的所有教师(Mrs. Smith在这种情况下)。我最初认为该HAVING条款将是完成此任务的正确方法,但我没有Mrs. Smith想到。
Mrs. Smith
HAVING
Teacher Student ------------------------- Mrs. Smith Danny Mrs. Smith Emily Mrs. Smith Todd Mrs. Smith Paul Mr. French Sam Mr. French Carol Mr. French Patty SELECT DISTINCT Teacher FROM Students HAVING (COUNT(Teacher) > 3) GROUP BY Teacher, Student
使用HAVING是正确的,您只需要正确使用它
SELECT Teacher FROM Students GROUP BY Teacher HAVING COUNT(Student) > 3
基本上,您是将 教师 记录分组在一起,同时计算每位教师有多少 学生 。并根据该数量进行过滤。