Postgresql 比较两个可能为空的字段
如果a.unitnum和b.unitnum都为null,是否存在a.unitnum=b.unitnum将为Postgresql 比较两个可能为空的字段,postgresql,Postgresql,如果a.unitnum和b.unitnum都为null,是否存在a.unitnum=b.unitnum将为true的比较运算符?似乎a.unitnum是b.unitnum无效否,但您可以使用a.unitnum=b.unitnum或(a.unitnum为null,b.unitnum为null)是,但是。以下是示例: t=# select null = null; ?column? ---------- (1 row) t=# set transform_null_equals = on; S
true
的比较运算符?似乎a.unitnum是b.unitnum无效否,但您可以使用a.unitnum=b.unitnum或(a.unitnum为null,b.unitnum为null)是,但是。以下是示例:
t=# select null = null;
?column?
----------
(1 row)
t=# set transform_null_equals = on;
SET
t=# select null = null;
?column?
----------
t
(1 row)
更新:显然只能用于比较column=NULL
,而不是column=column:
t=# with s as (select null::int a, null::int b) select a <> b from s;
?column?
----------
(1 row)
是的,有
如果您需要处理所有案件:
a.unitnum is null b.unitnum is null
a.unitnum is null b.unitnum is not null
a.unitnum is not null b.unitnum is null
a.unitnum is not null b.unitnum is not null
然后,您可能需要使用以下表达式:
select *
from a, b
where
((a.unitnum is not null) and (b.unitnum is not null) and (a.unitnum = b.unitnum)) or
((a.unitnum is null) and (b.unitnum is null));
在这里,您可以测试它的工作原理:
SELECT
((a is not null) and (b is not null) and (a = b)) or
((a is null) and (b is null))
FROM (VALUES (null,null)
, (null,1)
, (1,null)
, (1,1)
, (1,2)
) t1 (a, b);
附言。
仅使用
与公认答案没有区别。。。它的工作原理相同,但更短。kha!它没有比较两列:)我得到的合并类型text和integer无法匹配,我怀疑它是否可能匹配-不确定是否存在运算符text=integer
。无论如何,要进行比较,您需要合并(text\u colun,int\u column::text,'null')='null'
注意null或false
返回null
null或true
返回true
。您还应该知道null和true
返回null
null和false
返回false
。
a.unitnum is null b.unitnum is null
a.unitnum is null b.unitnum is not null
a.unitnum is not null b.unitnum is null
a.unitnum is not null b.unitnum is not null
select *
from a, b
where
((a.unitnum is not null) and (b.unitnum is not null) and (a.unitnum = b.unitnum)) or
((a.unitnum is null) and (b.unitnum is null));
SELECT
((a is not null) and (b is not null) and (a = b)) or
((a is null) and (b is null))
FROM (VALUES (null,null)
, (null,1)
, (1,null)
, (1,1)
, (1,2)
) t1 (a, b);