从三个具有相同id的表中选择

44u64gxh  于 2021-06-21  发布在  Mysql
关注(0)|答案(4)|浏览(287)

我有三张table

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 可能有空值。
我渴望得到你的答案。

8nuwlpux

8nuwlpux1#

下面是我能想到的最简单的答案。

SELECT T1.StudentdID,T1.Subject,T1.Semester_I_Mark,            
       T2.Semester_II_Mark, T3.Semester_III_Mark
FROM students_first_semester_mark T1, students_second_semester_mark T2, 
     students_third_semester_mark T3
JOIN T2 ON T1.StudentdID = T2.StudentdID
JOIN T3 ON T1.StudentdID = T3.StudentdID
jaql4c8m

jaql4c8m2#

这就是你想要的吗?

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]
hxzsmxv2

hxzsmxv23#

我假设所有的学生不会出现在所有科目。
在mysql中实现它有点棘手,因为它不支持 full outer join .
推导 FULL OUTER JOIN ,你需要一个 LEFT JOIN 工会 RIGHT JOIN . 在您的示例中,有3个表,您需要对前2个表重复此操作,然后合并它们。所以查询看起来会很复杂。
dbfiddle演示

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 .

n53p2ov0

n53p2ov04#

这可能会帮助您:

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

相关问题