BIT_AND 函数
功能描述
BIT_AND 函数用于计算指定表达式在一组数据中的按位与结果。该函数对整数类型的数据进行操作,包括 tinyint、smallint、int 和 bigint 类型。通过使用 BIT_AND 函数,可以对数据进行位运算,从而实现对数据的精确控制和处理。
参数说明
- expr (必需): 整数类型的表达式,包括 tinyint、smallint、int 和 bigint 类型。
- distinct (可选): 当设置为 distinct 时,函数将计算去重后的集合的按位与结果。
返回结果
- 返回值类型与参数类型一致。
- 如果设置了 distinct,则返回去重后的集合的按位与结果。
- null 值不参与计算。
使用示例
- 计算一组数据的按位与结果(未设置 distinct):
SELECT bit_and(col) FROM VALUES (3), (5), (7) AS tab(col);
+--------------+
| bit_and(col) |
+--------------+
| 1 |
+--------------+
- 计算一组数据的按位与结果(设置 distinct 并包含重复值):
SELECT bit_and(DISTINCT col) FROM VALUES (3), (3), (5), (7), (null) AS tab(col);
+-----------------------+
| bit_and(DISTINCT col) |
+-----------------------+
| 1 |
+-----------------------+
- 计算一组数据的按位与结果(包含 null 值):
SELECT bit_and(col) FROM VALUES (3), (null), (5), (7) AS tab(col);
+--------------+
| bit_and(col) |
+--------------+
| 1 |
+--------------+
- 计算不同整数类型的按位与结果:
SELECT bit_and(tinyint_col)col1, bit_and(smallint_col)col2, bit_and(int_col)col3, bit_and(bigint_col) col4
FROM VALUES (11, 22, 33, 44) AS t(tinyint_col, smallint_col, int_col, bigint_col);
+------+------+------+------+
| col1 | col2 | col3 | col4 |
+------+------+------+------+
| 11 | 22 | 33 | 44 |
+------+------+------+------+