选择每个班级的学生总数

时间:2016-04-08 16:46:10

标签: mysql

我有两张桌子:

类:cid,名称,位置

学生:sid,姓名,性别,classID

我正在尝试使用select语句来显示班级名称以及班级中的学生总数。我也试图排除桌上少于10名学生的班级。

2 个答案:

答案 0 :(得分:2)

  SELECT
    c.name as className,
    count(s.sid) as totalStudents
  FROM Class c
    JOIN Student s ON s.classID = c.cid
  GROUP BY c.id
  HAVING count(s.sid) >= 10

答案 1 :(得分:1)

select a.name, count(*)  
from Class as a
inner join Student as b on b.ClassID = a.cid
group by a.name
having count(*) >= 10;