2017-05-10 52 views
0

您好我有這樣
考試一表Mysql的加入並計算每個類別

id | exam_name 
1 | computer science 
2 | Environment science 

exam_students

id | exam_id | student_name 
1 | 1  | Josh 
2 | 1  | Michael 
3 | 1  | John 

我只需要加入和計算的總學生每個考試和輸出這樣的東西

exam_name   | total_students | 
computer science | 3    | 
Environment science| 0    | 

感謝您的幫助和建議

回答

1

試試這個

SELECT 
    a.exam_name, count(b.id) AS total_students 
FROM 
    exams a 
LEFT JOIN exam_students b ON a.id = b.exam_id 
GROUP BY 
    a.id 

希望這有助於

+0

未見其只給「計算機科學| 3「它會忽略環境科學 – sanu

+0

我更新了我的答案 – Iman