LeetCode(数据库)- 学生们参加各科测试的次数
Posted 程序员牧码
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了LeetCode(数据库)- 学生们参加各科测试的次数相关的知识,希望对你有一定的参考价值。
题目链接:点击打开链接
题目大意:略。
解题思路:略。
AC 代码
-- 解决方案(1)
select
ss.student_id as student_id,
ss.student_name as student_name,
ss.subject_name as subject_name,
ifnull(e1.attended_exams, 0) as attended_exams
from
(
select
*
from `Students` as s1
cross join `Subjects` as s2
) as ss
left join
(
select
student_id,
subject_name,
count(student_id) as attended_exams
from `Examinations`
group by student_id,subject_name
) as e1
on ss.student_id = e1.student_id
and ss.subject_name = e1.subject_name
order by ss.student_id, ss.subject_name
-- 解决方案(2)
SELECT s.student_id, student_name, sub.subject_name, COUNT(e.subject_name) attended_exams
FROM Subjects sub JOIN Students s LEFT JOIN Examinations e ON e.subject_name = sub.subject_name AND e.student_id = s.student_id
GROUP BY s.student_id, sub.subject_name
ORDER BY s.student_id, sub.subject_name
以上是关于LeetCode(数据库)- 学生们参加各科测试的次数的主要内容,如果未能解决你的问题,请参考以下文章
LeetCode:Database 76.学生们参加各科测试的次数