我有三张桌子
students_first_semester_mark (StudentdID,Subject, Semester_I_Mark )
students_second_semester_mark (StudentdID,Subject, Semester_II_Mark )
students_third_semester_mark (StudentdID,Subject, Semester_III_Mark )
我希望得到以下类型的输出:
_________________________________________________________________________________
|StudentdID | Subject | Semester_I_Mark| Semester_II_Mark | Semester_III_Mark
_________________________________________________________________________________
请注意,Semester_I_Mark
或Semester_II_Mark
可能包含空值。
我渴望得到你的答案。
答案 0 :(得分:0)
这是你想要的吗?
SELECT [students_first_semester_mark].[StudentdID]
,[students_first_semester_mark].[Subject]
,[students_first_semester_mark].[Semester_I_Mark]
,[students_second_semester_mark].[Semester_II_Mark]
,[students_third_semester_mark].[Semester_III_Mark]
FROM [students_first_semester_mark]
JOIN [students_second_semester_mark] ON [students_second_semester_mark].[StudentdID] = [students_first_semester_mark].[StudentdID]
JOIN [students_third_semester_mark] ON [students_third_semester_mark].[StudentdID] = [students_second_semester_mark].[StudentdID]
答案 1 :(得分:0)
我假设所有学生都不会出现在所有科目中。
在MySQL中实现它会有点棘手,因为它不支持full outer join
。
要导出FULL OUTER JOIN
,您需要LEFT JOIN
UNION RIGHT JOIN
。在你的情况下,有3个表,你需要重复前两个表,然后UNION它们。因此,查询将非常复杂。
select * from (
select coalesce(fs.studentdid,t.studentdid) as STUDENTDID
,coalesce(fs.subject,t.subject) as subject
,fs.semester_i_mark,fs.semester_ii_mark,t.semester_iii_mark
from
(
select
coalesce(f.studentdid,s.studentdid) as STUDENTDID,
coalesce(f.subject,s.subject) as subject
,f.semester_i_mark,s.semester_ii_mark
from
STUDENTS_FIRST_SEMESTER_MARK f
left join
STUDENTS_SECOND_SEMESTER_MARK s
on f.studentdid=s.studentdid
and f.subject=s.subject
UNION
select
coalesce(f.studentdid,s.studentdid) as STUDENTDID,
coalesce(f.subject,s.subject) as subject
,f.semester_i_mark,s.semester_ii_mark
from
STUDENTS_FIRST_SEMESTER_MARK f
right join
STUDENTS_SECOND_SEMESTER_MARK s
on f.studentdid=s.studentdid
and f.subject=s.subject
) fs
LEFT JOIN
STUDENTS_THIRD_SEMESTER_MARK t
on fs.STUDENTDID=t.studentdid
and fs.subject=t.subject
) u1
UNION
select * from (
select coalesce(fs.studentdid,t.studentdid) as STUDENTDID
,coalesce(fs.subject,t.subject) as subject
,fs.semester_i_mark,fs.semester_ii_mark,t.semester_iii_mark
from
(
select
coalesce(f.studentdid,s.studentdid) as STUDENTDID,
coalesce(f.subject,s.subject) as subject
,f.semester_i_mark,s.semester_ii_mark
from
STUDENTS_FIRST_SEMESTER_MARK f
left join
STUDENTS_SECOND_SEMESTER_MARK s
on f.studentdid=s.studentdid
and f.subject=s.subject
UNION
select
coalesce(f.studentdid,s.studentdid) as STUDENTDID,
coalesce(f.subject,s.subject) as subject
,f.semester_i_mark,s.semester_ii_mark
from
STUDENTS_FIRST_SEMESTER_MARK f
right join
STUDENTS_SECOND_SEMESTER_MARK s
on f.studentdid=s.studentdid
and f.subject=s.subject
) fs
RIGHT JOIN
STUDENTS_THIRD_SEMESTER_MARK t
on fs.STUDENTDID=t.studentdid
and fs.subject=t.subject
) u2
使用Coalesce,以便在我们使用studentdid
时返回subject
和outer join
的任何非空值。
答案 2 :(得分:0)
以下是我能想到的最简单的答案。
f5
答案 3 :(得分:0)
这可能会对您有所帮助:
SELECT table1.StudentdID, table1.Subject,
table1.Semester_I_Mark, table2.Semester_II_Mark,
table3.Semester_III_Mark
FROM students_first_semester_mark table1
INNER JOIN students_second_semester_mark table2
ON table1.StudentdID = table2.StudentdID
INNER JOIN students_third_semester_mark table3
ON table1.StudentdID = table3.StudentdID