Join 每个类别的Mysql连接和计数
嗨,我有一张这样的桌子Join 每个类别的Mysql连接和计数,join,mysqli,count,Join,Mysqli,Count,嗨,我有一张这样的桌子 考试 id | exam_name 1 | computer science 2 | Environment science id | exam_id | student_name 1 | 1 | Josh 2 | 1 | Michael 3 | 1 | John 考试学生 id | exam_name 1 | computer science 2 | Environment science id | exam_id
考试
id | exam_name
1 | computer science
2 | Environment science
id | exam_id | student_name
1 | 1 | Josh
2 | 1 | Michael
3 | 1 | John
考试学生
id | exam_name
1 | computer science
2 | Environment science
id | exam_id | student_name
1 | 1 | Josh
2 | 1 | Michael
3 | 1 | John
我只需要加入并计算每次考试的学生总数,然后输出如下内容
exam_name | total_students |
computer science | 3 |
Environment science| 0 |
感谢您的帮助和建议试试这个
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
希望这有帮助不,它只会给“计算机科学”一个答案,它会忽略环境科学