给出以下查询
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
时,我反而观察到空结果集。
我对该查询的第一个版本没有产生两行,并且通过否定比较没有产生第一个查询返回的集合的补集而感到有些困惑。
到目前为止,我还无法在文档中找到描述此行为的正确位置,并且希望有任何正确方向的指示,以便更好地理解正在发生的情况。
就上下文而言,当我尝试检查外连接中是否有匹配的行时,我就想到了这一点。为了避免上述有些令人惊讶的行为,我选择检查连接关系的不可空列之一是否为空/不为空,这解决了我眼前的问题,但让我想知道是否有更好的方法来执行这种检查总是有效的,即使您要加入的关系的所有列都可以为空,并且不必枚举所有列并依赖其中至少一列为非空。
状况
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 |
以及可能的检查
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 |
select *
from jsonb_to_recordset('[{},{"a":1},{"b":1},{"a":2,"b":42}]') i(a int, b int)
where i is null
a | b |
---|---|
空 | 空 |
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 |