如果 JOINED 值为 null,则 LEFT Join 不会从 main table 中提取记录
LEFT Join does not pull record from main table if the JOINED value is null
假设我有一个 tableA 看起来像这样:
ID Name IssueID
1 Bob 2
2 John Null
我有一个名为 tblRef 的参考 table,其中包括以下列:
IssueID IssueDesc
2 Not Completed
3 Completed
我的查询看起来像这样,
Select a.Id,a.Name, I.IssueDesc from tableA a Left Join tblRef I on a.issueId =
I.IssueDesc where IssueID not in (2)
如果我这样做,我看不到 tableA 中 IssueID 为 NULL 的记录,有什么办法可以提取它吗?或者更确切地说,为什么它没有拉高记录?
NULL
"fails"几乎都是比较。实际发生的是结果是 NULL
,并且行被过滤掉。
在SQL服务器中,您需要明确测试:
where IssueID not in (2) OR IssueID is null
请记住,NULL
的语义通常表示 "unknown" 值而不是 "missing" 值。当您有 NULL not in (2)
时,语义是“不在 (2) 中”。结果是"unknown",因为值可能是2。
假设我有一个 tableA 看起来像这样:
ID Name IssueID
1 Bob 2
2 John Null
我有一个名为 tblRef 的参考 table,其中包括以下列:
IssueID IssueDesc
2 Not Completed
3 Completed
我的查询看起来像这样,
Select a.Id,a.Name, I.IssueDesc from tableA a Left Join tblRef I on a.issueId =
I.IssueDesc where IssueID not in (2)
如果我这样做,我看不到 tableA 中 IssueID 为 NULL 的记录,有什么办法可以提取它吗?或者更确切地说,为什么它没有拉高记录?
NULL
"fails"几乎都是比较。实际发生的是结果是 NULL
,并且行被过滤掉。
在SQL服务器中,您需要明确测试:
where IssueID not in (2) OR IssueID is null
请记住,NULL
的语义通常表示 "unknown" 值而不是 "missing" 值。当您有 NULL not in (2)
时,语义是“不在 (2) 中”。结果是"unknown",因为值可能是2。