我正在处理表列中的可为空值,我想知道为什么会这样:
with cte as (
select 'c1'::text as c1,null::text as c2
union
select 'c2'::text as c1,null::text as c2
)
select *
from cte
where c2 is null
返回一个结果集,其中两个“行”在 c2 中具有空值;与此查询相比:
with cte as (
select 'c1'::text as c1,null::text as c2
union
select 'c2'::text as c1,null::text as c2
)
select *
from cte
where c2 = null
返回一个空的结果集!!
所以看起来这field is null
实际上与field = null
? 问题是......是否有“空兼容运算符”可以使用?所以我可以像这样写比较:column_name = $1
并且参数$1
可以是非空值或空值。IF$1
是一个空值,那么它在语义上等于写为IS NULL
, ELSE(如果$1
不是空值)那么它在语义上等于= $1
。
PD:这个问题也适用于案件is NOT NULL
与<> NULL