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
上一篇:海之胜商城系统开发


下一篇:oracle SQL 执行进度