BOOL_AND

对一组布尔值执行逻辑与(AND)操作,返回操作后的布尔结果。

命令格式

BOOLEAN BOOL_AND(BOOLEAN <expr>)

参数说明

  • expr:必填。BOOLEAN类型表达式。

返回值说明

返回BOOLEAN类型。返回规则如下:

  • 若输入的expr中所有值都为true,则返回true,否则返回false。

  • 若输入的expr中包含为NULL的值,NULL值不参与计算。

使用示例

  • 示例一:执行简单逻辑与操作。

    SELECT BOOL_AND(colname) FROM VALUES (true), (false), (true) AS tab(colname);
    -- 返回结果
    +------+
    | _c0  | 
    +------+
    | false | 
    +------+
    
    SELECT BOOL_AND(colname1) FROM VALUES (true, 1), (false, 2), (true, 1) AS tab(colname1, colname2);
    -- 返回结果
    +------+
    | _c0  | 
    +------+
    | false | 
    +------+
  • 示例二:数组内存在NULL值,将会忽略组内的NULL值。

    SELECT BOOL_AND(colname) FROM VALUES (NULL), (true), (true) AS tab(colname);
    -- 返回结果
    +------+
    | _c0  | 
    +------+
    | true | 
    +------+
  • 示例三:计算colname1列中所有满足colname2=1的行的逻辑与(AND)结果。

    SELECT BOOL_AND(colname1) FILTER(WHERE colname2 = 1) FROM VALUES (true, 1), (false, 2), (true, 1) AS tab(colname1, colname2);
    -- 返回结果
    +------+
    | _c0  | 
    +------+
    | true | 
    +------+