题目描述
(通过次数14,017 | 提交次数27,980,通过率50.10%)
学生表: Students +---------------+---------+ | Column Name | Type | +---------------+---------+ | student_id | int | | student_name | varchar | +---------------+---------+ 主键为 student_id(学生ID),该表内的每一行都记录有学校一名学生的信息。 科目表: Subjects +--------------+---------+ | Column Name | Type | +--------------+---------+ | subject_name | varchar | +--------------+---------+ 主键为 subject_name(科目名称),每一行记录学校的一门科目名称。 考试表: Examinations +--------------+---------+ | Column Name | Type | +--------------+---------+ | student_id | int | | subject_name | varchar | +--------------+---------+ 这张表压根没有主键,可能会有重复行。 学生表里的一个学生修读科目表里的每一门科目,而这张考试表的每一行记录就表示学生表里的某个学生参加了一次科目表里某门科目的测试。 要求写一段 SQL 语句,查询出每个学生参加每一门科目测试的次数,结果按 student_id 和 subject_name 排序。 查询结构格式如下所示: Students table: +------------+--------------+ | student_id | student_name | +------------+--------------+ | 1 | Alice | | 2 | Bob | | 13 | John | | 6 | Alex | +------------+--------------+ Subjects table: +--------------+ | subject_name | +--------------+ | Math | | Physics | | Programming | +--------------+ Examinations table: +------------+--------------+ | student_id | subject_name | +------------+--------------+ | 1 | Math | | 1 | Physics | | 1 | Programming | | 2 | Programming | | 1 | Physics | | 1 | Math | | 13 | Math | | 13 | Programming | | 13 | Physics | | 2 | Math | | 1 | Math | +------------+--------------+ Result table: +------------+--------------+--------------+----------------+ | student_id | student_name | subject_name | attended_exams | +------------+--------------+--------------+----------------+ | 1 | Alice | Math | 3 | | 1 | Alice | Physics | 2 | | 1 | Alice | Programming | 1 | | 2 | Bob | Math | 1 | | 2 | Bob | Physics | 0 | | 2 | Bob | Programming | 1 | | 6 | Alex | Math | 0 | | 6 | Alex | Physics | 0 | | 6 | Alex | Programming | 0 | | 13 | John | Math | 1 | | 13 | John | Physics | 1 | | 13 | John | Programming | 1 | +------------+--------------+--------------+----------------+ 结果表需包含所有学生和所有科目(即便测试次数为0): Alice 参加了 3 次数学测试, 2 次物理测试,以及 1 次编程测试; Bob 参加了 1 次数学测试, 1 次编程测试,没有参加物理测试; Alex 啥测试都没参加; John 参加了数学、物理、编程测试各 1 次。 来源:力扣(LeetCode) 链接:https://leetcode.cn/problems/students-and-examinations
//测试数据 Create table If Not Exists Students (student_id int, student_name varchar(20)); Create table If Not Exists Subjects (subject_name varchar(20)); Create table If Not Exists Examinations (student_id int, subject_name varchar(20)); insert into Students (student_id, student_name) values ('1', 'Alice'); insert into Students (student_id, student_name) values ('2', 'Bob'); insert into Students (student_id, student_name) values ('13', 'John'); insert into Students (student_id, student_name) values ('6', 'Alex'); insert into Subjects (subject_name) values ('Math'); insert into Subjects (subject_name) values ('Physics'); insert into Subjects (subject_name) values ('Programming'); insert into Examinations (student_id, subject_name) values ('1', 'Math'); insert into Examinations (student_id, subject_name) values ('1', 'Physics'); insert into Examinations (student_id, subject_name) values ('1', 'Programming'); insert into Examinations (student_id, subject_name) values ('2', 'Programming'); insert into Examinations (student_id, subject_name) values ('1', 'Physics'); insert into Examinations (student_id, subject_name) values ('1', 'Math'); insert into Examinations (student_id, subject_name) values ('13', 'Math'); insert into Examinations (student_id, subject_name) values ('13', 'Programming'); insert into Examinations (student_id, subject_name) values ('13', 'Physics'); insert into Examinations (student_id, subject_name) values ('2', 'Math'); insert into Examinations (student_id, subject_name) values ('1', 'Math');
解题思路
Students表保存了所有学生信息。
Subjects表保存了所有课程信息。
Examinations表保存了学生参加的课程考试记录。
题目要求:查询每个学生参加每一门课程考试的次数。
这里要求的是学生与课程的所有组合的次数,即使学生从未参加过那门课程的考试。
那么,就需要对Students表与Subjects表做笛卡尔积。一般的方法是将两张表内关联,然后使用一个永远为真的关联条件进行关联。
最后,再左关联Examinations表,并按学生及课程做分组汇总即可。
参考SQL
未特别说明的情况下,参考SQL为基于MySQL8.0实现。
select a.student_id, max(a.student_name) student_name, b.subject_name, coalesce(count(c.student_id),0) attended_exams from Students a inner join Subjects b on 1=1 left join Examinations c on a.student_id = c.student_id and b.subject_name = c.subject_name group by a.student_id,b.subject_name order by a.student_id,b.subject_name;
本站所有内容均为原创,本站保留所有权利。仅允许非商业用途的转载,但必须注明来源网站、作者、来源链接!否则,由此造成的一切后果,由转载方承担!
干货分享、技术提升、面试笔试、学习交流,欢迎关注公众号:xuesql。QQ学习交流群:209942678。