简体   繁体   English

mySQL SELECT左外部联接涉及两个以上的表

[英]mySQL SELECT Left Outer Join involving more than 2 tables

Given the following tables: 给出下表:

Students((PK)studentID, firstName, lastName, gender, dateOfBirth) 学生((PK)studentID,名字,姓氏,性别,出生日期)

Courses((PK)courseCode, courseName, classLevel, credit) 课程((PK)课程代码,课程名称,课程级别,学分)

Registration((PK,FK)studentID, (PK,FK)courseCode, grade) 注册((PK,FK)学生ID,(PK,FK)课程代码,年级)

I'd like to write a query that grabs the info for every student ( Students.studentID , Students.firstName , Students.lastName ), their course info ( Courses.courseName ), and grade ( Registration.grade ), including students who aren't registered for classes returning null values in the courseName and grade columns for those students. 我想编写抓住信息为每一个学生(Students.studentID,Students.firstName,Students.lastName),查询他们的课程信息(Courses.courseName)和等级(Registration.grade),包括谁AREN学生没有为在课程名和成绩列中为这些学生返回空值的课程注册

From what I know (I'm still learning a lot) this will require something like: 据我所知(我仍在学习很多),这将需要类似以下内容:

Students LEFT OUTER JOIN [some aggregate of Courses and Registration]

But I'm not sure how to format it using mySQL syntax. 但是我不确定如何使用mySQL语法格式化它。

Here what I've got: 这是我所拥有的:

SELECT S.studentID, S.firstName, S.lastName, C.courseName, R.grade
    FROM Courses AS C, Students AS S
    LEFT OUTER JOIN Registration AS R ON S.studentID = R.studentID
    AND R.courseCode = C.courseCode
ORDER BY S.studentID
;

I know its not right I get a 'Unknown Column C.courseCode' error, so the syntax is obviously off, but this is as close as I could get to what I think it needs to look like. 我知道这是不对的,我收到“ Unknown Column C.courseCode”错误,因此语法显然已关闭,但这与我认为需要的样子非常接近。

Is this what you need? 这是您需要的吗? (I prefer LEFT JOIN because it is shorter than LEFT OUTER JOIN , but they are the same.) (我更喜欢LEFT JOIN因为它比LEFT OUTER JOIN短,但它们是相同的。)

SELECT S.studentID, S.firstName, S.lastName, C.courseName, R.grade
    FROM Students AS S
    LEFT JOIN Registration AS R ON S.studentID = R.studentID
    LEFT JOIN Courses AS C ON R.courseCode = C.courseCode
ORDER BY S.studentID
;

Edit: Like Gordon, I prefer explicit joins as you can see from my answer. 编辑:像戈登一样,我更喜欢显式联接,正如您从我的答案中看到的那样。 However, to my knowledge there is no difference in performance. 但是,据我所知,性能没有差异。

When you use left join , start with table that contains all the rows you want to keep. 当使用left join ,从包含要保留的所有行的表开始。 In this case, students . 在这种情况下, students Then use left join on all the other tables. 然后在所有其他表上使用left join联接。

Also, never use commas in the from clause. 另外, 切勿from子句中使用逗号。 Always use explicit join syntax. 始终使用显式join语法。

So, your query should look like: 因此,您的查询应类似于:

SELECT S.studentID, S.firstName, S.lastName, C.courseName, R.grade
FROM Students S LEFT OUTER JOIN
     Registration R
     ON S.studentID = R.studentID LEFT OUTER JOIN
     Courses AS C, 
     ON R.courseCode = C.courseCode
ORDER BY S.studentID;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM