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

Him*_*han 3 sql postgresql boolean-logic aggregate-functions

假设我有下表:

id   column_a  column_b   column_c
1     t          f           t
2     t          f           f
3     f          t           f
Run Code Online (Sandbox Code Playgroud)

从上表中,我要:

select rows from id = 1,2;
Run Code Online (Sandbox Code Playgroud)

结果应为:

column_a   column_b   column_c
 t          f            t
Run Code Online (Sandbox Code Playgroud)

如果定义的ID中的任何行对特定列都为true,则我们假设结果为true。

Erw*_*ter 5

使用聚合函数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);
Run Code Online (Sandbox Code Playgroud)

手册:

如果至少一个输入值是true,则为true,否则为false