Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 检查Postgres复合字段是否为null/空_Sql_Postgresql_Null_Composite - Fatal编程技术网

Sql 检查Postgres复合字段是否为null/空

Sql 检查Postgres复合字段是否为null/空,sql,postgresql,null,composite,Sql,Postgresql,Null,Composite,使用,基本上可以构建一个字段,其结构定义为另一个表。我有一个名为“recipient”的复合字段,类型为“person”。在我的特定场景中,此收件人字段通常为空。检查复合字段是否为空的正确方法是什么。我试过: select * from bla where recipient is not null select * from bla where recipient is null select * from bla where recipient = null select * from bl

使用,基本上可以构建一个字段,其结构定义为另一个表。我有一个名为“recipient”的复合字段,类型为“person”。在我的特定场景中,此收件人字段通常为空。检查复合字段是否为空的正确方法是什么。我试过:

select * from bla where recipient is not null
select * from bla where recipient is null
select * from bla where recipient = null
select * from bla where recipient != null
在所有这些情况下,它都不会返回任何内容。那么,如何正确地检查复合值是否为空

更新

再读几遍之后,这似乎是我的问题:

有人可能会认为
!(x为空)=x不为空
在所有情况下均为真。但也有一个例外——复合类型。当复合值的一个字段为
NULL
且另一个字段为
notnull
时,两个运算符的结果均为false
为空
仅当所有字段均为
NULL
时才为真
不为空
仅当所有字段均为
不为空
时才为真。对于两者之间的任何情况,则两个运算符都返回false


我确实有一些字段是空的,而其他字段不是空的。我希望该字段将被视为非空,如果复合字段中的任何项都不是空的。。。当它们都不是空的时候就不是了。除了检查每个字段之外,还有其他方法吗?

为NULL
不为NULL
也适用于复杂类型,因此这两种方法应该是合适的:

select * from bla where recipient is not null
select * from bla where recipient is null
若要捕获组合值(行/记录)的并非所有字段都为空的情况,请执行以下操作:

SELECT *
FROM   bla
WHERE  NOT (recipient IS NULL);
结果:

 recipient | all_null | all_notnull | some_notnull | some_null
-----------+----------+-------------+--------------+-----------
 (foo,1)   | f        | t           | t            | f
 (,2)      | f        | f           | t            | t
 (,)       | t        | f           | f            | t

相关的:


为[not]null
只能计算为
真/假
,因此(在我看来)
不是真的
是不必要的,只要
不是(收件人为[not]null)
就足够了。我错过了什么?@ClodoaldoNeto:你是对的,做同样的事更简单。我用您的输入更新了答案。请注意,
为空
/
不为空
对于复杂/行/记录类型是特殊的。
 recipient | all_null | all_notnull | some_notnull | some_null
-----------+----------+-------------+--------------+-----------
 (foo,1)   | f        | t           | t            | f
 (,2)      | f        | f           | t            | t
 (,)       | t        | f           | f            | t