2016-04-08 84 views
0

我有兩個表:選擇學生的總人數每班

類:CID,名稱,位置

學生:SID,姓名,性別,CLASSID

我試圖使用選擇語句以顯示班級名稱以及班級中的學生總數。我還試圖排除少於10名學生的班級。

回答

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
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;