只有当值存在时,MySQL才会与一列联接
在我的查询中,表上有一组连接,如下所示:只有当值存在时,MySQL才会与一列联接,mysql,Mysql,在我的查询中,表上有一组连接,如下所示: SELECT a.id, a.firstnames, a.surname,b.subject_grade_id FROM students a LEFT JOIN student_subject b ON a.id=b.studentid LEFT JOIN subject_grade c on b.subject_grade_id=c.id WHERE b.subject_grade_id=? ORDER by a.surname ASC id
SELECT a.id, a.firstnames, a.surname,b.subject_grade_id
FROM students a
LEFT JOIN student_subject b ON a.id=b.studentid
LEFT JOIN subject_grade c on b.subject_grade_id=c.id
WHERE b.subject_grade_id=?
ORDER by a.surname ASC
id firstnames surname subject_grade_id
---------------------------------------------
8 John Doe 17
9 Bob Mitchell 17
10 Mary Smith 17
id term studentid subjectid mark
---------------------------------------------
1 2 8 17 25
id firstnames surname subject_grade_id mark
-----------------------------------------------------
8 John Doe 17 25
9 Bob Mitchell 17 null
10 Mary Smith 17 null
id firstnames surname subject_grade_id mark
--------------------------------------------------------
8 John Doe 17 25
9 Bob Mitchell 17 25
10 Mary Smith 17 25
这给了我这样一个结果:
SELECT a.id, a.firstnames, a.surname,b.subject_grade_id
FROM students a
LEFT JOIN student_subject b ON a.id=b.studentid
LEFT JOIN subject_grade c on b.subject_grade_id=c.id
WHERE b.subject_grade_id=?
ORDER by a.surname ASC
id firstnames surname subject_grade_id
---------------------------------------------
8 John Doe 17
9 Bob Mitchell 17
10 Mary Smith 17
id term studentid subjectid mark
---------------------------------------------
1 2 8 17 25
id firstnames surname subject_grade_id mark
-----------------------------------------------------
8 John Doe 17 25
9 Bob Mitchell 17 null
10 Mary Smith 17 null
id firstnames surname subject_grade_id mark
--------------------------------------------------------
8 John Doe 17 25
9 Bob Mitchell 17 25
10 Mary Smith 17 25
现在,我还有一个marks
表,我想将其加入到我的查询中。假设该表只有一个值,如下所示:
SELECT a.id, a.firstnames, a.surname,b.subject_grade_id
FROM students a
LEFT JOIN student_subject b ON a.id=b.studentid
LEFT JOIN subject_grade c on b.subject_grade_id=c.id
WHERE b.subject_grade_id=?
ORDER by a.surname ASC
id firstnames surname subject_grade_id
---------------------------------------------
8 John Doe 17
9 Bob Mitchell 17
10 Mary Smith 17
id term studentid subjectid mark
---------------------------------------------
1 2 8 17 25
id firstnames surname subject_grade_id mark
-----------------------------------------------------
8 John Doe 17 25
9 Bob Mitchell 17 null
10 Mary Smith 17 null
id firstnames surname subject_grade_id mark
--------------------------------------------------------
8 John Doe 17 25
9 Bob Mitchell 17 25
10 Mary Smith 17 25
我的预期输出如下:
SELECT a.id, a.firstnames, a.surname,b.subject_grade_id
FROM students a
LEFT JOIN student_subject b ON a.id=b.studentid
LEFT JOIN subject_grade c on b.subject_grade_id=c.id
WHERE b.subject_grade_id=?
ORDER by a.surname ASC
id firstnames surname subject_grade_id
---------------------------------------------
8 John Doe 17
9 Bob Mitchell 17
10 Mary Smith 17
id term studentid subjectid mark
---------------------------------------------
1 2 8 17 25
id firstnames surname subject_grade_id mark
-----------------------------------------------------
8 John Doe 17 25
9 Bob Mitchell 17 null
10 Mary Smith 17 null
id firstnames surname subject_grade_id mark
--------------------------------------------------------
8 John Doe 17 25
9 Bob Mitchell 17 25
10 Mary Smith 17 25
我怎样才能做到这一点
我试过这个:
select a.id, a.firstnames, a.surname,b.subject_grade_id,d.mark
from students a
left join student_subject b ON a.id=b.studentid
left join subject_grade c on b.subject_grade_id=c.id
outer join marks d on d.subjectid=b.subject_grade_id
WHERE b.subject_grade_id=17
ORDER by a.surname ASC
但它给了我这样一个结果:
SELECT a.id, a.firstnames, a.surname,b.subject_grade_id
FROM students a
LEFT JOIN student_subject b ON a.id=b.studentid
LEFT JOIN subject_grade c on b.subject_grade_id=c.id
WHERE b.subject_grade_id=?
ORDER by a.surname ASC
id firstnames surname subject_grade_id
---------------------------------------------
8 John Doe 17
9 Bob Mitchell 17
10 Mary Smith 17
id term studentid subjectid mark
---------------------------------------------
1 2 8 17 25
id firstnames surname subject_grade_id mark
-----------------------------------------------------
8 John Doe 17 25
9 Bob Mitchell 17 null
10 Mary Smith 17 null
id firstnames surname subject_grade_id mark
--------------------------------------------------------
8 John Doe 17 25
9 Bob Mitchell 17 25
10 Mary Smith 17 25
我做错了什么?不要
外部连接
您的标记
表,左连接
它。LEFT JOIN
的目的是在不满足ON
条件时保留JOIN右侧的行,并用NULL
值替换JOIN左侧的值
select a.id, a.firstnames, a.surname,b.subject_grade_id,d.mark
from students a
left join student_subject b ON a.id=b.studentid
left join subject_grade c on b.subject_grade_id=c.id
left join marks d on d.id=a.id // You don't need to make join on subject Id just need to join on student id
WHERE b.subject_grade_id=17
ORDER by a.surname ASC
您可以尝试上面的查询。向
连接标记d
添加一个条件,将其连接到表a
,例如和a.id=d.studentid
,我明白了!这起作用了。我现在明白我的错误了。非常感谢!非常感谢。我现在更明白了。