一、题目
二、代码
# Write your MySQL query statement below
#初步成形 有部分学生有科目没考考
-- select Students.student_id , Students.student_name ,Examinations.subject_name , count(Examinations.subject_name) as subject_name from Students
-- right join Examinations
-- on Examinations.student_id = Students.student_id
-- group by Students.student_id,Examinations.subject_name
-- order by Students.student_id, Examinations.subject_name asc
-- select Examinations.subject_name , Examinations.student_id from Subjects
-- left join Examinations
-- on Subjects.subject_name = Examinations.subject_name
#自连接 不用条件可以直接连接对吗
select table1.student_id, table1.student_name ,table1.subject_name, coalesce(attended_exams, 0) as attended_exams from
(
select * from Students
join Subjects
order by Students.student_id,Subjects.subject_name asc
) as table1
left join
(
select student_id , subject_name ,count(subject_name ) as attended_exams from Examinations
group by student_id, subject_name
) as table2
on table1.student_id = table2.student_id
and table1.subject_name = table2.subject_name
order by table1.student_id, table1.subject_name asc