以下是给定的表格:
`student` `subject`
------------------- -------------------
id name id subject
------------------- -------------------
1 Alice 1 Maths
2 Bob 2 Science
3 Eve 3 Economics
------------------- -------------------
`marks`
-----------------------------------------------------
id student_id subject_id marks
-----------------------------------------------------
1 1 1 30
2 1 2 40
3 2 3 50
4 3 1 60
5 3 2 70
-----------------------------------------------------
我需要一个输出,如下所示:
`o/p`
----------------------------------------------
name subject marks
----------------------------------------------
Alice Maths 30
Alice Science 10
Alice Economics NULL
Bob Maths NULL
Bob Science NULL
Bob Economics 50
Eve Maths 60
Eve Science 70
Eve Economics NULL
----------------------------------------------
请注意,我的目标是MySQL 5.6.x语法,我创建了一个上面here的SQL小提琴,以便于访问问题。
答案 0 :(得分:3)
你去吧
select
st.name,
su.subject,
m.marks
from student as st
cross join subject as su
left join marks as m on m.student_id=st.id and m.subject_id=su.id
order by st.name, su.subject
答案 1 :(得分:-1)
SELECT student.name,
subject.subject,
marks.marks
FROM student
JOIN subject ON student.id = subject.id
JOIN marks ON student.id = marks.id
ORDER BY student.name,
subject.subject