查询使用INNER JOIN链接4个表

时间:2017-09-02 10:35:36

标签: mysql sql inner-join

我有以下4个表格:

student_info:

S_ID naam   email   telefoon locatie U_ID
   1 Walter Walter@ 03938    Home       1

student_combi:

S_ID V_ID
   1    1

student_vak:

V_ID vak  R_ID
   1 HTML    1

student_richting:

R_ID丰富       1网络开发

我想使用INNER JOIN查询SELECT'vak'(来自'student_vak'表)和'richting'(来自'student_richting'表)

到目前为止,我有这个:

SELECT student_vak.vak,student_richting.richting
FROM student_vak
INNER JOIN student_richting ON student_vak.R_ID = student_richting.R_ID
INNER JOIN student_combi ON student_info.S_ID = student_combi.S_ID
INNER JOIN student_vak ON student_combi.V_ID = student_vak.V_ID
INNER JOIN student_richting ON student_vak.R_ID = student_richting.R_ID

如果你知道答案,请提前感谢。

1 个答案:

答案 0 :(得分:2)

以下内容将满足您的需求。

SELECT S.S_Id Student_Id, S.Naam Student_Name, SV.vak, SR.richting
FROM Student_Info S 
INNER JOIN Student_Combi SC ON S.S_id  = SC.S_Id
INNER JOIN Student_Vak SV ON SC.V_Id = SV.V_Id
INNER JOIN student_richting SR ON SV.R_Id = SR.R_Id

- 实施例

CREATE TABLE #student_vak
(V_ID   INT,
 vak        NVARCHAR(256),
 R_ID   INT)

INSERT INTO #student_vak
SELECT '1', 'HTML','1'

CREATE TABLE #student_richting
(R_ID       INT,
richting    NVARCHAR(256)
)
INSERT INTO #student_richting
SELECT '1', 'Web-Development'

SELECT SV.vak, SR.richting
FROM #student_vak SV
INNER JOIN #student_richting SR ON SV.R_Id = SR.R_Id