【发布时间】:2018-09-26 07:58:42
【问题描述】:
我正在比较两个表格并得到一个奇怪的结果。我开始比较两个通常为空的列。声明 null = null 总是返回 false。我发现这些列的 make is NULL 有效。结果显示很多双行,看不懂。
所以基本上这行data.data_1
column1 column2 column3 column4
apple tree NULL NULL
还有这一行data.data_2
column1 column2 column3 column4
apple tree NULL NULL
在以下查询后返回
select
br.column1, br.column2, br.column3, br.column4,
af.column1, af.column2
from
data.data_1 as br
left join data.data_2 as af on
(br.column1 = af.column1 and
br.column2 = af.column2 and
br.column3 IS NULL and af.column3 IS NULL and
br.column4 IS NULL and af.column4 IS NULL)
column1 column2 column3 column4 af.column1 af.column2
apple tree NULL NULL apple tree
apple tree NULL NULL apple tree
apple tree NULL NULL apple tree
apple tree NULL NULL apple tree
apple tree NULL NULL apple tree
apple tree NULL NULL apple tree
apple tree NULL NULL apple tree
apple tree NULL NULL apple tree
apple tree NULL NULL apple tree
apple tree NULL NULL apple tree
apple tree NULL NULL apple tree
apple tree NULL NULL apple tree
apple tree NULL NULL apple tree
apple tree NULL NULL apple tree
apple tree NULL NULL apple tree
而不是我希望它返回的内容是:
column1 column2 column3 column4 af.column1 af.column2
apple tree NULL NULL apple tree
我可以在它周围添加一个独特的功能,但我觉得这将是一个不必要的额外操作。
【问题讨论】:
-
NULL 可能会造成混淆。这篇文章有帮助吗? xaprb.com/blog/2006/05/18/…
-
或许你想比较一下
AND ((br.column3 IS NULL AND af.column3 IS NULL) OR (br.column3 = af.column3)) -
你说
double values是什么意思? -
null = null不返回 false。它返回未知。记住这一点很重要,因为not (null = null)也是未知(而且绝对不是真的) -
结果与样本数据不符。
标签: sql postgresql null