如果任何源列为真,则将布尔值聚合为真

Aggregate boolean values to true if any of the source columns is true

假设我有以下 table:

id   column_a  column_b   column_c
1     t          f           t
2     t          f           f
3     f          t           f

从上面table,我想:

select rows from id = 1,2;

结果应该是:

column_a   column_b   column_c
 t          f            t

如果定义的 id 中的任何行对于特定列为真,我们假设结果为真。

使用聚合函数bool_or()

SELECT bool_or(column_a) AS column_a
     , bool_or(column_b) AS column_b
     , bool_or(column_c) AS column_c
FROM   tbl
WHERE  id IN (1,2);

手册:

true if at least one input value is true, otherwise false

bool_or(...)绝对是你最好的朋友。

只有一件事要记住哪个手册没有提到:如果提供给 bool_or 的所有值都是 null,结果也将是 null