要求寫一段 SQL 語句倦青,查詢出每個學(xué)生參加每一門科目測試的次數(shù),結(jié)果按 student_id 和 subject_name 排序芜茵。結(jié)果如下
代碼:
select st.student_id,st.student_name,su.subject_name, count(su.subject_name) as attended_exams
from
students st cross join subjects su
left join Examinations as e
on e.student_id = st.student_id
group by st.student_id,su.subject_name
order by st.student_id , su.subject_name
學(xué)到新【知識點】cross join 返回兩表的笛卡爾積蜻懦,具體如下