SQL Server LEFT JOIN 和 WHERE 子句 [英] SQL Server LEFT JOIN and WHERE clause
本文介绍了SQL Server LEFT JOIN 和 WHERE 子句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是我的代码
SELECT ID, Name, Phone
FROM Table1
LEFT JOIN Table2 ON Table1.ID = Table2.ID
WHERE Table1.ID = 12 AND Table2.IsDefault = 1
问题发生在 Table2 为 null 时,因此查询没有返回任何内容.
The problem happens when Table2 is null, so, the query returns nothing.
如何让查询的最后一部分 AND Table2.IsDefault = 1
可选?
How do I leave the last part of the query AND Table2.IsDefault = 1
optional?
我尝试使用 OR
来缩短查询,但我发现它的工作方式与 C# 不同
I've tried to short circuit the query using OR
but I've found out that it works different than C#
推荐答案
SELECT ID, Name, Phone
FROM Table1
LEFT JOIN Table2
ON Table1.ID = Table2.ID AND Table2.IsDefault = 1
WHERE Table1.ID = 12
这篇关于SQL Server LEFT JOIN 和 WHERE 子句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文