Postgresql SQL:如何用 null 和 True,False 值检查布尔字段?
Postgresql SQL: How check boolean field with null and True,False Value?
在我的数据库中 table 我有一个布尔列。与 will False、True 和 Null 有一些交易。
这些是我尝试过的案例:
案例:1
select * from table_name where
boolean_column is null;
效果很好。给出该列的所有事务都具有空值的结果。
案例:2
select *from table_name where boolean_column = False;
效果很好。给出该列的所有交易都具有 False 值的结果。
案例:3
这是行不通的要求。我希望所有交易都具有值 False 和 Null。
这些我都试过了。
i) select *from table_name where boolean_column is False or Null;
只给出 False 的结果,不显示空记录。
ii) select *from table_name where boolean_column is Null or False;
*它只给出 null 的结果,它不显示具有 False 值的记录。 *
iii) select *from table_name where boolean_column is Null or boolean_column = False;
这只是显示所有交易根本没有应用任何条件。
如何解决这个问题。
任何指导表示赞赏。
提前致谢。
Rgds,
阿尼尔.
我对 Postgres 的内部运作不够了解,不知道为什么您在 WHERE
子句中使用双重条件的查询不起作用。但是解决这个问题的一种方法是使用您知道有效的两个查询中的 UNION
:
SELECT * FROM table_name WHERE boolean_column IS NULL
UNION
SELECT * FROM table_name WHERE boolean_column = FALSE
您也可以尝试使用 COALESCE
:
SELECT * FROM table_name WHERE COALESCE(boolean_column, FALSE) = FALSE
第二个查询将用 FALSE
替换所有 NULL
值,然后与 WHERE
条件下的 FALSE
进行比较。
在 PostgreSQL 上您可以使用:
SELECT * FROM table_name WHERE (boolean_column IS NULL OR NOT boolean_column)
select *from table_name where boolean_column is False or Null;
被解释为“( boolean_column 为 False ) 或 (null)”。
它 returns 只有 boolean_column
为假的行,因为第二个条件始终为假。
select *from table_name where boolean_column is Null or False;
同理。解释为“(boolean_column 为 Null) 或 (False)”
select *from table_name where boolean_column is Null or boolean_column = False;
这个有效并且 returns 2 行:false
和 null
。
我刚刚创建了 table 来确认。你可能在某处打错了字。
PG中boolean有3种状态:true、false和unknown(null)。此处解释:Postgres boolean datatype
因此您只需要查询 NOT TRUE:
SELECT * from table_name WHERE boolean_column IS NOT TRUE;
将此恢复到 post DISTINCT FROM
选项,该选项自 Postgres 8 以来一直存在。该方法类似于 Brad Dre 的回答。在您的情况下,您的 select 类似于
SELECT *
FROM table_name
WHERE boolean_column IS DISTINCT FROM TRUE
在我的数据库中 table 我有一个布尔列。与 will False、True 和 Null 有一些交易。
这些是我尝试过的案例:
案例:1
select * from table_name where
boolean_column is null;
效果很好。给出该列的所有事务都具有空值的结果。
案例:2
select *from table_name where boolean_column = False;
效果很好。给出该列的所有交易都具有 False 值的结果。
案例:3 这是行不通的要求。我希望所有交易都具有值 False 和 Null。
这些我都试过了。
i) select *from table_name where boolean_column is False or Null;
只给出 False 的结果,不显示空记录。
ii) select *from table_name where boolean_column is Null or False;
*它只给出 null 的结果,它不显示具有 False 值的记录。 *
iii) select *from table_name where boolean_column is Null or boolean_column = False;
这只是显示所有交易根本没有应用任何条件。
如何解决这个问题。 任何指导表示赞赏。
提前致谢。
Rgds, 阿尼尔.
我对 Postgres 的内部运作不够了解,不知道为什么您在 WHERE
子句中使用双重条件的查询不起作用。但是解决这个问题的一种方法是使用您知道有效的两个查询中的 UNION
:
SELECT * FROM table_name WHERE boolean_column IS NULL
UNION
SELECT * FROM table_name WHERE boolean_column = FALSE
您也可以尝试使用 COALESCE
:
SELECT * FROM table_name WHERE COALESCE(boolean_column, FALSE) = FALSE
第二个查询将用 FALSE
替换所有 NULL
值,然后与 WHERE
条件下的 FALSE
进行比较。
在 PostgreSQL 上您可以使用:
SELECT * FROM table_name WHERE (boolean_column IS NULL OR NOT boolean_column)
select *from table_name where boolean_column is False or Null;
被解释为“( boolean_column 为 False ) 或 (null)”。
它 returns 只有
boolean_column
为假的行,因为第二个条件始终为假。select *from table_name where boolean_column is Null or False;
同理。解释为“(boolean_column 为 Null) 或 (False)”
select *from table_name where boolean_column is Null or boolean_column = False;
这个有效并且 returns 2 行:
false
和null
。
我刚刚创建了 table 来确认。你可能在某处打错了字。
PG中boolean有3种状态:true、false和unknown(null)。此处解释:Postgres boolean datatype
因此您只需要查询 NOT TRUE:
SELECT * from table_name WHERE boolean_column IS NOT TRUE;
将此恢复到 post DISTINCT FROM
选项,该选项自 Postgres 8 以来一直存在。该方法类似于 Brad Dre 的回答。在您的情况下,您的 select 类似于
SELECT *
FROM table_name
WHERE boolean_column IS DISTINCT FROM TRUE