8.4 中文镜像

MySQL 8.4 参考手册 · 中文镜像

正文为中文,英文原文默认隐藏。点击任意中文段落即可展开 / 收起对应英文原文。

打开官方英文原文 ↗

译文由机器翻译生成,仅供理解参考;有疑问请以官方英文原文为准。

中文点击中文段落可显示 / 隐藏英文原文本页译文覆盖 100%

11.1.5 位值字面量11.1.5 Bit-Value Literals

位值字面量使用 b'val' 或 0bval 表示法书写。val 是由 0 和 1 构成的二进制值。任何前导 b 的字母大小写无关紧要。前导 0b 是区分大小写的,不能写成 0B。Bit-value literals are written using b'val' or 0bval notation. val is a binary value written using zeros and ones. Lettercase of any leading b does not matter. A leading 0b is case-sensitive and cannot be written as 0B.

合法的位值字面量:Legal bit-value literals:

b'01'
B'01'
0b01

非法的位值字面量:Illegal bit-value literals:

b'2'    (2 is not a binary digit)
0B01    (0B must be written as 0b)

默认情况下,位值字面量是二进制字符串:By default, a bit-value literal is a binary string:

mysql> SELECT b'1000001', CHARSET(b'1000001');
+------------+---------------------+
| b'1000001' | CHARSET(b'1000001') |
+------------+---------------------+
| A          | binary              |
+------------+---------------------+
mysql> SELECT 0b1100001, CHARSET(0b1100001);
+-----------+--------------------+
| 0b1100001 | CHARSET(0b1100001) |
+-----------+--------------------+
| a         | binary             |
+-----------+--------------------+

位值字面量可以有一个可选的字符集引导符和 COLLATE 子句,以将其指定为使用特定字符集和排序规则的字符串:A bit-value literal may have an optional character set introducer and COLLATE clause, to designate it as a string that uses a particular character set and collation:

[_charset_name] b'val' [COLLATE collation_name]

示例:Examples:

SELECT _latin1 b'1000001';
SELECT _utf8mb4 0b1000001 COLLATE utf8mb4_danish_ci;

示例使用 b'val' 表示法,但 0bval 表示法也允许使用引导符。有关引导符的信息,请参阅 12.3.8 节,“字符集引导符”。The examples use b'val' notation, but 0bval notation permits introducers as well. For information about introducers, see Section 12.3.8, “Character Set Introducers”.

在数字上下文中,MySQL 将位字面量视为整数。为了确保位字面量以数字方式处理,请在数字上下文中使用它。实现此目的的方法包括加 0 或使用 CAST(... AS UNSIGNED)。例如,分配给用户定义变量的位字面量默认是二进制字符串。要以数字方式分配该值,请在数字上下文中使用它:In numeric contexts, MySQL treats a bit literal like an integer. To ensure numeric treatment of a bit literal, use it in numeric context. Ways to do this include adding 0 or using CAST(... AS UNSIGNED). For example, a bit literal assigned to a user-defined variable is a binary string by default. To assign the value as a number, use it in numeric context:

mysql> SET @v1 = b'1100001';
mysql> SET @v2 = b'1100001'+0;
mysql> SET @v3 = CAST(b'1100001' AS UNSIGNED);
mysql> SELECT @v1, @v2, @v3;
+------+------+------+
| @v1  | @v2  | @v3  |
+------+------+------+
| a    |   97 |   97 |
+------+------+------+

空位值(b'')计算为零长度二进制字符串。转换为数字时,它产生 0:An empty bit value (b'') evaluates to a zero-length binary string. Converted to a number, it produces 0:

mysql> SELECT CHARSET(b''), LENGTH(b'');
+--------------+-------------+
| CHARSET(b'') | LENGTH(b'') |
+--------------+-------------+
| binary       |           0 |
+--------------+-------------+
mysql> SELECT b''+0;
+-------+
| b''+0 |
+-------+
|     0 |
+-------+

位值表示法便于指定要分配给 BIT 列的值:Bit-value notation is convenient for specifying values to be assigned to BIT columns:

mysql> CREATE TABLE t (b BIT(8));
mysql> INSERT INTO t SET b = b'11111111';
mysql> INSERT INTO t SET b = b'1010';
mysql> INSERT INTO t SET b = b'0101';

结果集中位值作为二进制值返回,可能无法很好显示。要将位值转换为可打印形式,可以在数字上下文中使用它,或使用转换函数,例如 BIN() 或 HEX()。转换后的值不会显示高位的 0。Bit values in result sets are returned as binary values, which may not display well. To convert a bit value to printable form, use it in numeric context or use a conversion function such as BIN() or HEX(). High-order 0 digits are not displayed in the converted value.

mysql> SELECT b+0, BIN(b), OCT(b), HEX(b) FROM t;
+------+----------+--------+--------+
| b+0  | BIN(b)   | OCT(b) | HEX(b) |
+------+----------+--------+--------+
|  255 | 11111111 | 377    | FF     |
|   10 | 1010     | 12     | A      |
|    5 | 101      | 5      | 5      |
+------+----------+--------+--------+

对于位字面量,位运算被视为数字上下文,但在 MySQL 8.4 及更高版本中,位运算允许使用数字或二进制字符串参数。要显式指定位字面量的二进制字符串上下文,请对至少一个参数使用 _binary 引导符:For bit literals, bit operations are considered numeric context, but bit operations permit numeric or binary string arguments in MySQL 8.4 and higher. To explicitly specify binary string context for bit literals, use a _binary introducer for at least one of the arguments:

mysql> SET @v1 = b'000010101' | b'000101010';
mysql> SET @v2 = _binary b'000010101' | _binary b'000101010';
mysql> SELECT HEX(@v1), HEX(@v2);
+----------+----------+
| HEX(@v1) | HEX(@v2) |
+----------+----------+
| 3F       | 003F     |
+----------+----------+

显示的结果对于这两种位操作看起来相似,但没有 _binary 的结果是 BIGINT 值,而使用 _binary 的结果是二进制字符串。由于结果类型的不同,显示的值也不同:数字结果不会显示高位的 0。The displayed result appears similar for both bit operations, but the result without _binary is a BIGINT value, whereas the result with _binary is a binary string. Due to the difference in result types, the displayed values differ: High-order 0 digits are not displayed for the numeric result.