即使值为null,也显示结果

时间:2012-11-12 01:27:13

标签: mysql

Select CH.id, CH.studentId, ST.name, CH.recordId, RC.name 
from checklist CH, student ST, record RC
Where CH.studentId=ST.id and CH.recordId=RC.id

上面是使用mysql的查询。上面的查询的问题是当CH.recordId的值为null时,它不会显示任何结果。现在我的问题是如何显示结果,即使CH.recordId为空?在mysql结果选项卡中,CH.recordId和RC.name列将只为null。

感谢。

2 个答案:

答案 0 :(得分:1)

使用显式连接语法,以便您可以执行left join

select CH.id, CH.studentId, ST.name, CH.recordId, RC.name
from checklist CH
join student ST
  on ST.id = CH.studentId
left join record RC
  on RC.id = CH.recordId

答案 1 :(得分:0)

LEFT JOIN表而不是INNER JOIN,并使用新的SQL-92语法,如下所示:

SELECT CH.id, CH.studentId, ST.name, CH.recordId, RC.name 
FROM checklist CH
LEFT JOIN student ST ON CH.StudentId = ST.id
LEFT JOIN record RC ON CH.recordId = RC.id