BOOL_AND 函数
bool_and([distinct] expr)
功能描述
BOOL_AND 函数用于判断一组布尔值(expr)是否全部为 true。当所有给定的布尔值都为 true 时,函数返回 true;否则,返回 false。如果设置了 distinct 关键字,函数将仅计算不重复的布尔值。
参数说明
返回类型
布尔值(boolean)。
使用示例
- 判断所有布尔值为 true:
SELECT bool_and(col) FROM VALUES (true), (true), (true) AS tab(col);
+---------------+
| bool_and(col) |
+---------------+
| true |
+---------------+
- 包含 null 值的情况:
SELECT bool_and(col) FROM VALUES (true), (true), (null) AS tab(col);
+---------------+
| bool_and(col) |
+---------------+
| true |
+---------------+
- 使用 distinct 关键字,去重后判断所有布尔值为 true:
SELECT bool_and(DISTINCT col) FROM VALUES (true), (true), (true) AS tab(col);
+------------------------+
| bool_and(DISTINCT col) |
+------------------------+
| true |
+------------------------+
- 包含 false 值的情况:
SELECT bool_and(col) FROM VALUES (false), (true), (null) AS tab(col);
+---------------+
| bool_and(col) |
+---------------+
| false |
+---------------+
- 仅包含一个 true 值和一个 false 值:
SELECT bool_and(col) FROM VALUES (true), (false) AS tab(col);
+---------------+
| bool_and(col) |
+---------------+
| false |
+---------------+