如果a.unitnum和b.unitnum都为空,是否存在a.unitnum = b.unitnum为true
的比较运算符?似乎a.unitnum IS b.unitnum无效
答案 0 :(得分:6)
是的,有IS DISTINCT FROM
and IS NOT DISTINCT FROM
postgres=# \pset null ****
Null display is "****".
postgres=# select null = null;
┌──────────┐
│ ?column? │
╞══════════╡
│ **** │
└──────────┘
(1 row)
postgres=# select null is not distinct from null;
┌──────────┐
│ ?column? │
╞══════════╡
│ t │
└──────────┘
(1 row)
postgres=# select 10 = null;
┌──────────┐
│ ?column? │
╞══════════╡
│ **** │
└──────────┘
(1 row)
postgres=# select 10 is distinct from null;
┌──────────┐
│ ?column? │
╞══════════╡
│ t │
└──────────┘
(1 row)
postgres=# select 10 is not distinct from null;
┌──────────┐
│ ?column? │
╞══════════╡
│ f │
└──────────┘
(1 row)
postgres=# select 10 is not distinct from 20;
┌──────────┐
│ ?column? │
╞══════════╡
│ f │
└──────────┘
(1 row)
答案 1 :(得分:2)
IF(a.unitnum IS null AND b.unitnum IS null)
THEN
RAISE NOTICE 'unitum field is null in both a and b tables'
ELSE
RAISE NOTICE 'unitum field is not null in at least one a or b tables'
END IF;
答案 2 :(得分:1)
是,there is,但是it is recomended to not use it。这是样本:
t=# select null = null;
?column?
----------
(1 row)
t=# set transform_null_equals = on;
SET
t=# select null = null;
?column?
----------
t
(1 row)
更新:显然只适用于比较column = NULL
,而不是列=列:
t=# with s as (select null::int a, null::int b) select a <> b from s;
?column?
----------
(1 row)
因此最短的比较将是合并的:
t=# with s as (select null::int a, null::int b) select coalesce(a,b,0) = 0 from s;
?column?
----------
t
(1 row)
答案 3 :(得分:0)
否但您可以使用a.unitnum = b.unitnum或(a.unitnum为null且b.unitnum为null)
答案 4 :(得分:0)
如果您需要处理所有情况:
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);
附言
只需使用已接受答案中的 IS NOT DISTINCT FROM
......它的工作原理相同但更短。