其他分享
首页 > 其他分享> > 1280. 学生们参加各科测试的次数

1280. 学生们参加各科测试的次数

作者:互联网

这道题关键在于不是所有学生都参加所有科目的考试,我们也不能很快速的知道选了哪些科目。
这里用了一个CROSS JOIN,即笛卡尔积,不需要内联结既可以联结两张表。

SELECT s1.student_id, 
       s1.student_name, 
       s2.subject_name, 
       COUNT(e.subject_name) AS attended_exams
FROM Students AS s1 
CROSS JOIN Subjects AS s2
LEFT JOIN Examinations AS e 
ON s1.student_id = e.student_id
AND s2.subject_name = e.subject_name
GROUP BY s1.student_id, s2.subject_name
ORDER BY s1.student_id, s2.subject_name

标签:1280,s2,s1,name,次数,student,各科,id,subject
来源: https://blog.csdn.net/Helslie/article/details/104916214