仅在表2具有值时联接 [英] Join only when table 2 has a value

查看:66
本文介绍了仅在表2具有值时联接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以组成一段SQL从两个表中检索数据,其中第二个表可能有值也可能没有值.

例如

Is it possible to compose a piece of SQL to retrive data from two tables where the second table may or may not have a value.

e.g.

 Select t1.name, t2.diplomatype
from studentmaster t1, resultmaster t2
where t1.result = t2.result
or t1.result is null



所需的输出量
姓名DiplomaType
---- -----------
汤姆·帕斯
玛丽·霍诺斯(Mary Honours)
杰克
丽兹通行证

...或者我是否需要使用存储的过程?



Desired Ouitput
Name DiplomaType
---- -----------
Tom Pass
Mary Honours
Jack
Liz Pass

... Or do I need to use a stored proocedure?

推荐答案

是的,当然,请使用MySQL外连接.

参见此处 [
Yes offcourse Use MySQL Outer JOIN.

See Here[^]


SELECT *
FROM
StudentMaster T1
    LEFT JOIN RESULTMASTER T2 ON
       T1.Result = T2.Result


您可以使用外部联接:

You may use an Outer Join:

Select t1.name, t2.diplomatype
from studentmaster t1 LEFT OUTER JOIN resultmaster t2
on  t1.code = t2.code
where 1=1



您必须将连接条件放在ON表达式中,然后完成.



You must put the join conditions in the ON expression, and it''s done.


这篇关于仅在表2具有值时联接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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