核心内容摘要
从火影女神到流量风暴:纲手“同人事件”引发热议,二次元边界究竟在哪?
代码SELECT s.student_id, s.student_name, sub.subject_name, COUNT(e.subject_name) AS attended_exams FROM Students s CROSS JOIN Subjects sub LEFT JOIN Examinations e ON s.student_id e.student_id AND sub.subject_name e.subject_name GROUP BY s.student_id, s.student_name, sub.subject_name ORDER BY s.student_id, sub.subject_name; 分步解释
CROSS JOIN Students 和 Subjects这一步就是“配对所有学生和所有科目”。
比如 Alice Math、Alice Physics、Bob Math……一个不漏。
✅ 这样就保证了“即使没考试也要列出”。
LEFT JOIN Examinations把考试记录“贴”到上面的配对上。
如果某学生某科有考试记录就加上如果没有那一行的考试信息就是NULL。
用LEFT JOIN是为了保留左边的所有配对不会丢掉没考试的组合。
COUNT(e.subject_name)统计每个学生-科目组合实际考了多少次。
为什么不是COUNT(*)因为COUNT(*)会把“没考试”的行也数成 1它数的是“行数”。
而COUNT(e.subject_name)只数非空值——没考试时e.subject_name是NULL就不计入结果就是 0。
✅
GROUP BY因为我们用了COUNT()聚合函数所以必须按学生科目分组。
否则数据库不知道“到底要统计谁的考试次数”。
ORDER BY题目要求按student_id和subject_name排序所以加上这句让结果整齐。
结果解题步骤