mysql left join选择左右表中的所有记录,即使rigth表中没有记录也是如此 [英] mysql left join select all records from left and right table even if no records in rigth table

查看:634
本文介绍了mysql left join选择左右表中的所有记录,即使rigth表中没有记录也是如此的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在MYSQL中有3个表,如:

rec_application => AppId(PK),AppNum

rec_shortlistedCandidates => ShortListId(PK),AppId(FK) ,总分数

rec_shortListedScore => AppId(FK),TestId,分数,评论





我想要列出入选候选人的AppNum,分数和评论选择的TestId [在下拉列表更改] .TestId可以或不可以在'rec_shortListedScore'中,如果不存在则则Score = 0且Comment = null。



i试过:

SELECT S.AppId,SL.Score,SL.Comments,A.AppNum



FROM rec_shortlistedcandidates S



INNER JOIN rec_application A ON A.AppId = S.AppId

left JOIN rec_shortlistscore SL ON SL.AppId = S.AppId;



但仅当'rec_shortlistscore'中包含数据时才显示。

I have 3 tables in MYSQL like:
rec_application=>AppId(PK),AppNum
rec_shortlistedCandidates=>ShortListId(PK),AppId(FK),TotalScore
rec_shortListedScore=>AppId(FK),TestId,Score,Comment


I want to list the Shortlisted candidates with there AppNum,Score and Comment for the selected TestId[on a dropdown change].The TestId can or can't be there in the 'rec_shortListedScore',if not present then Score=0 and Comment=null.

i tried :
SELECT S.AppId,SL.Score,SL.Comments,A.AppNum

FROM rec_shortlistedcandidates S

INNER JOIN rec_application A ON A.AppId=S.AppId
left JOIN rec_shortlistscore SL ON SL.AppId=S.AppId;

but this displays only when the 'rec_shortlistscore' have data in it.

推荐答案

左外连接将执行此操作。它返回右表中的所有记录,如果左表没有匹配则返回空值



SQL Wizardry第一部分 - 加入 [ ^ ]是我的第一篇SQL文章,它解释了它。
A left outer join will do this. It returns all records from the right table, with nulls if the left table has no match

SQL Wizardry Part One - Joins[^] is my first SQL article, and it explains it.


这篇关于mysql left join选择左右表中的所有记录,即使rigth表中没有记录也是如此的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆