PostgreSQL 布尔比较

PostgreSQL boolean comparison

下面对比有区别吗

#1

where x = true

#2

where x is true

#3

where x = '1'

#4

where x

使用isis not 将处理值为空的情况。如果依赖=,比较的结果也会是null

select null is true as "is", null = true as "equal";
 is | equal
----+-------
 f  |
(1 row)
  1. 这与

    完全一样
    WHERE x
    

    TRUEFALSENULL 正好是 x

  2. 这与第一种情况相同,除了当x为NULL时,在这种情况下它将是FALSE。所以和

    一样
    WHERE coalesce(x, FALSE)
    
  3. 这恰好与第一种情况相同,因为'1'被解释为TRUE。见 the documentation:

    The datatype input function for type boolean accepts these string representations for the “true” state:

    true
    yes
    on
    1

我喜欢的方法是最简单的:

WHERE x