“from_item is not null”/“from_item is null”的语义是什么?

问题描述 投票:0回答:1

给出以下查询

select *
from jsonb_to_recordset('[{"a":1},{"a":2,"b":42}]') i(a int, b int)
where i is not null

我观察单行的结果,该行的两列都不为空。

当否定 where 子句为

where i is null
时,我反而观察到空结果集。

我对该查询的第一个版本没有产生两行,并且通过否定比较没有产生第一个查询返回的集合的补集而感到有些困惑。

到目前为止,我还无法在文档中找到描述此行为的正确位置,并且希望有任何正确方向的指示,以便更好地理解正在发生的情况。

就上下文而言,当我尝试检查外连接中是否有匹配的行时,我就想到了这一点。为了避免上述有些令人惊讶的行为,我选择检查连接关系的不可空列之一是否为空/不为空,这解决了我眼前的问题,但让我想知道是否有更好的方法来执行这种检查总是有效的,即使您要加入的关系的所有列都可以为空,并且不必枚举所有列并依赖其中至少一列为非空。

postgresql outer-join
1个回答
0
投票

状况

i is not null
->
a is not null and b is not null

状况
i is null
->
a is null and b is null

有查询

select *
from jsonb_to_recordset('[{},{"a":1},{"b":1},{"a":2,"b":42}]') i(a int, b int)

输出是

a b
1
1
2 42

以及可能的检查

  1. 不为空
select *
from jsonb_to_recordset('[{},{"a":1},{"b":1},{"a":2,"b":42}]') i(a int, b int)
where i is not null
a b
2 42
  1. 不为空
select *
from jsonb_to_recordset('[{},{"a":1},{"b":1},{"a":2,"b":42}]') i(a int, b int)
 where i is null
a b
  1. 合并
select *
from jsonb_to_recordset('[{},{"a":1},{"b":1},{"a":2,"b":42}]') i(a int, b int)
 where coalesce(a,b) is not null
a b
1
1
2 42
© www.soinside.com 2019 - 2024. All rights reserved.