天天看点

mysql select left [outer] join_mySQL SELECT Left Outer Join涉及2个以上的表(mySQL SELECT Left Outer Join inv...

mySQL SELECT Left Outer Join涉及2个以上的表(mySQL SELECT Left Outer Join involving more than 2 tables)

给出以下表格:

学生((PK)studentID,firstName,lastName,gender,dateOfBirth)

课程((PK)courseCode,courseName,classLevel,credit)

注册((PK,FK)studentID,(PK,FK)courseCode,年级)

我想写一个查询来抓取每个学生的信息( Students.studentID , Students.firstName , Students.lastName ),他们的课程信息( Courses.courseName )和成绩( Registration.grade ),包括那些学生为在这些学生的courseName和grade列中返回null值的类注册 。

据我所知(我还在学习很多),这将需要以下内容:

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

但我不确定如何使用mySQL语法格式化它。

在这里我得到了:

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

;

我知道它不正确我得到一个'Unknown Column C.courseCode'错误,所以语法显然是关闭的,但这是我能够达到我认为它需要的样子。

Given the following tables:

Students((PK)studentID, firstName, lastName, gender, dateOfBirth)

Courses((PK)courseCode, courseName, classLevel, credit)

Registration((PK,FK)studentID, (PK,FK)courseCode, grade)

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.

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.

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.

原文:https://stackoverflow.com/questions/33535156

更新时间:2020-10-17 09:10

最满意答案

使用left join ,从包含要保留的所有行的表开始。 在这种情况下, students 。 然后在所有其他表上使用left join 。

此外, 永远不要在from子句中使用逗号。 始终使用显式join语法。

因此,您的查询应如下所示:

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;

When you use left join, start with table that contains all the rows you want to keep. In this case, students. Then use left join on all the other tables.

Also, never use commas in the from clause. Always use explicit join syntax.

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;

2015-11-05

相关问答

从from子句中省略括号: FROM (aspnet_Users LEFT OUTER JOIN UserProfile ON aspnet_User.UserName = UserProfile.UserName)

你应该做 : FROM aspnet_Users LEFT OUTER JOIN UserProfile ON aspnet_User.UserName = UserProfile.UserName

Leave out the parenthesis from the from

...

如果我正确地收集OP,问题是您需要两次加入applicant表,以便它在列表列中显示两次。 一组是加入DT记录的申请人记录,一组是加入DICT记录的申请人记录。 SELECT DICT.*,dict_applicant.*,dt_applicant.*,DT.*

FROM DICT

INNER JOIN applicant AS dict_applicant ON DICT.applicant = dict_applicant.id_applicant

LEFT OUTER JOIN DT on

...

SELECT d.SeqNo

, d.SeqHeader

, d.SeqText

, h.UniqueID

, h.Room

, h.Status

, h.Water

, h.AuditBy

, v.Status

FROM NXLHR_SequenceNo_default d

LEFT

JOIN NXLHR_Hist h

ON h.SeqID = d.SeqID

AND h.Uniqu

...

因此,第一个LEFT OUTER JOIN获取玩家表,然后添加每个玩家经理的信息。 每个玩家的经理都有一个ID ,他也是一个有ID的玩家。 如果A先生(带有player_id 9 )是Ted的经理,其中player_id 5 ,则Ted的manager_id将为9 。 第一次加入需要Ted的manager_id , 9 ,并将其与其经理A先生的player_id匹配,以便经理的信息现在也在桌面上, m.last_name和m.first_name将显示A先生的名字。 第二个连接使用team_id并

...

简单的规则:不要在from子句中使用逗号: SELECT *

FROM GA LEFT OUTER JOIN

MOZ

ON GA.Page = MOZ.URL LEFT OUTER JOIN

SF

ON GA.Page = SF.Address;

您的版本在三个表格之间表达完整的笛卡尔积, 然后尝试执行left join 。 具体问题是表别名不明确,因为它们在from子句中定义了两次。 Simple rule: Don't use commas in th

...

我猜你把GROUP BY放在了WHERE之后,这是非法的。 请参阅MySQL中SELECT语法的参考资料 。 尝试这个: SELECT

s.id AS id,

s.story_date AS datetime,

s.story_content AS content,

t.story_type_label AS type_label,

t.story_type_slug AS type_slug,

COUNT(c.id) AS comment_coun

...

我认为您的非自然版本中存在拼写错误,您要比较的是: SELECT * FROM table1 NATURAL LEFT OUTER JOIN table2;

SELECT * FROM table1 LEFT OUTER JOIN table2 USING ( person );

首先,我希望两者的结果相等。** 其次,在您的情况下,我没有看到非NATURAL / USING版本中的任何一点,因为您只有一列共同且它们始终被命名。 **我打算用“关系式”来限定(例如列顺序和行顺序不相关),但OU

...

看起来设计可能需要一些工作。 但是现在,你可以这样做: SELECT

a.column1, a.column2

FROM

(SELECT * FROM table_a1

UNION ALL SELECT * FROM table_a2

UNION ALL SELECT * FROM table_a3) AS a

LEFT OUTER JOIN

(SELECT * FROM table_b1

UNION ALL SELECT * FROM table_b2) A

...

尝试这个: SELECT `t`.`id` AS `t0_c0`

, `news`.`id` AS `t2_c0`

, `news`.`title` AS `t2_c1`

, `news`.`url` AS `t2_c2`

, `news`.`date` AS `t2_c3`

, `news`.`hash` AS `t2_c4`

, `news`.`list_id` AS `t2_c5`

FROM

`words` `t`

LE

...

使用left join ,从包含要保留的所有行的表开始。 在这种情况下, students 。 然后在所有其他表上使用left join 。 此外, 永远不要在from子句中使用逗号。 始终使用显式join语法。 因此,您的查询应如下所示: SELECT S.studentID, S.firstName, S.lastName, C.courseName, R.grade

FROM Students S LEFT OUTER JOIN

Registration R

ON S.s

...