<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="zh-Hans-CN">
	<id>http://wiki.eijux.com/index.php?action=history&amp;feed=atom&amp;title=MySQL_%E5%87%BD%E6%95%B0%E5%92%8C%E8%BF%90%E7%AE%97%E7%AC%A6%EF%BC%9A%E4%BD%8D%E5%87%BD%E6%95%B0%E5%92%8C%E8%BF%90%E7%AE%97%E7%AC%A6</id>
	<title>MySQL 函数和运算符：位函数和运算符 - 版本历史</title>
	<link rel="self" type="application/atom+xml" href="http://wiki.eijux.com/index.php?action=history&amp;feed=atom&amp;title=MySQL_%E5%87%BD%E6%95%B0%E5%92%8C%E8%BF%90%E7%AE%97%E7%AC%A6%EF%BC%9A%E4%BD%8D%E5%87%BD%E6%95%B0%E5%92%8C%E8%BF%90%E7%AE%97%E7%AC%A6"/>
	<link rel="alternate" type="text/html" href="http://wiki.eijux.com/index.php?title=MySQL_%E5%87%BD%E6%95%B0%E5%92%8C%E8%BF%90%E7%AE%97%E7%AC%A6%EF%BC%9A%E4%BD%8D%E5%87%BD%E6%95%B0%E5%92%8C%E8%BF%90%E7%AE%97%E7%AC%A6&amp;action=history"/>
	<updated>2026-05-02T11:30:23Z</updated>
	<subtitle>本wiki上该页面的版本历史</subtitle>
	<generator>MediaWiki 1.38.2</generator>
	<entry>
		<id>http://wiki.eijux.com/index.php?title=MySQL_%E5%87%BD%E6%95%B0%E5%92%8C%E8%BF%90%E7%AE%97%E7%AC%A6%EF%BC%9A%E4%BD%8D%E5%87%BD%E6%95%B0%E5%92%8C%E8%BF%90%E7%AE%97%E7%AC%A6&amp;diff=6528&amp;oldid=prev</id>
		<title>2023年3月31日 (五) 15:09 Eijux</title>
		<link rel="alternate" type="text/html" href="http://wiki.eijux.com/index.php?title=MySQL_%E5%87%BD%E6%95%B0%E5%92%8C%E8%BF%90%E7%AE%97%E7%AC%A6%EF%BC%9A%E4%BD%8D%E5%87%BD%E6%95%B0%E5%92%8C%E8%BF%90%E7%AE%97%E7%AC%A6&amp;diff=6528&amp;oldid=prev"/>
		<updated>2023-03-31T15:09:51Z</updated>

		<summary type="html">&lt;p&gt;&lt;/p&gt;
&lt;table style=&quot;background-color: #fff; color: #202122;&quot; data-mw=&quot;interface&quot;&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;tr class=&quot;diff-title&quot; lang=&quot;zh-Hans-CN&quot;&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;←上一版本&lt;/td&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;2023年3月31日 (五) 23:09的版本&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot; id=&quot;mw-diff-left-l1&quot;&gt;第1行：&lt;/td&gt;
&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;第1行：&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;−&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;[[category:&lt;del style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;MySQL&lt;/del&gt;]]&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;+&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;[[category:&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;MySQL文档&lt;/ins&gt;]]&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br/&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br/&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br/&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br/&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;</summary>
		<author><name>Eijux</name></author>
	</entry>
	<entry>
		<id>http://wiki.eijux.com/index.php?title=MySQL_%E5%87%BD%E6%95%B0%E5%92%8C%E8%BF%90%E7%AE%97%E7%AC%A6%EF%BC%9A%E4%BD%8D%E5%87%BD%E6%95%B0%E5%92%8C%E8%BF%90%E7%AE%97%E7%AC%A6&amp;diff=2232&amp;oldid=prev</id>
		<title>Eijux：​建立内容为“category:MySQL   位函数和运算符： {| class=&quot;wikitable&quot; ! Name !! Description  |- | &amp; || 位运算“AND” &lt;syntaxhighlight lang=&quot;xml&quot;&gt; mysql&gt; SELECT…”的新页面</title>
		<link rel="alternate" type="text/html" href="http://wiki.eijux.com/index.php?title=MySQL_%E5%87%BD%E6%95%B0%E5%92%8C%E8%BF%90%E7%AE%97%E7%AC%A6%EF%BC%9A%E4%BD%8D%E5%87%BD%E6%95%B0%E5%92%8C%E8%BF%90%E7%AE%97%E7%AC%A6&amp;diff=2232&amp;oldid=prev"/>
		<updated>2021-04-02T05:42:44Z</updated>

		<summary type="html">&lt;p&gt;建立内容为“&lt;a href=&quot;/%E5%88%86%E7%B1%BB:MySQL&quot; title=&quot;分类:MySQL&quot;&gt;category:MySQL&lt;/a&gt;   位函数和运算符： {| class=&amp;quot;wikitable&amp;quot; ! Name !! Description  |- | &amp;amp; || 位运算“AND” &amp;lt;syntaxhighlight lang=&amp;quot;xml&amp;quot;&amp;gt; mysql&amp;gt; SELECT…”的新页面&lt;/p&gt;
&lt;p&gt;&lt;b&gt;新页面&lt;/b&gt;&lt;/p&gt;&lt;div&gt;[[category:MySQL]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
位函数和运算符：&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! Name !! Description &lt;br /&gt;
|-&lt;br /&gt;
| &amp;amp; || 位运算“AND”&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;xml&amp;quot;&amp;gt;&lt;br /&gt;
mysql&amp;gt; SELECT 29 &amp;amp; 15;&lt;br /&gt;
        -&amp;gt; 13&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
结果是一个无符号的 64 位整数。&lt;br /&gt;
|-&lt;br /&gt;
| &amp;gt;&amp;gt; || 右移&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;xml&amp;quot;&amp;gt;&lt;br /&gt;
mysql&amp;gt; SELECT 4 &amp;gt;&amp;gt; 2;&lt;br /&gt;
        -&amp;gt; 1&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
结果是一个无符号的 64 位整数。该值被截断为 64 位。特别是，如果移位计数大于或等于一个无符号 64 位数字的宽度，则结果为零。&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;&amp;lt; || 左移&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;xml&amp;quot;&amp;gt;&lt;br /&gt;
mysql&amp;gt; SELECT 1 &amp;lt;&amp;lt; 2;&lt;br /&gt;
        -&amp;gt; 4&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
结果是一个无符号的 64 位整数。该值被截断为 64 位。特别是，如果移位计数大于或等于一个无符号 64 位数字的宽度，则结果为零。&lt;br /&gt;
|-&lt;br /&gt;
| ^ || 位运算“XOR”&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;xml&amp;quot;&amp;gt;&lt;br /&gt;
mysql&amp;gt; SELECT 1 ^ 1;&lt;br /&gt;
        -&amp;gt; 0&lt;br /&gt;
mysql&amp;gt; SELECT 1 ^ 0;&lt;br /&gt;
        -&amp;gt; 1&lt;br /&gt;
mysql&amp;gt; SELECT 11 ^ 3;&lt;br /&gt;
        -&amp;gt; 8&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
结果是一个无符号的 64 位整数。&lt;br /&gt;
|-&lt;br /&gt;
| BIT_COUNT() || 返回设置的位数&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;xml&amp;quot;&amp;gt;&lt;br /&gt;
mysql&amp;gt; SELECT BIT_COUNT(29), BIT_COUNT(b'101010');&lt;br /&gt;
        -&amp;gt; 4, 3&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
返回参数“N”中设置为无符号 64 位整数的位数，如果参数为NULL则返回NULL。&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;nowiki&amp;gt;|&amp;lt;/nowiki&amp;gt; || 位运算“OR”&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;xml&amp;quot;&amp;gt;&lt;br /&gt;
mysql&amp;gt; SELECT 29 | 15;&lt;br /&gt;
        -&amp;gt; 31&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
结果是一个无符号的 64 位整数。&lt;br /&gt;
|-&lt;br /&gt;
| ~ || 位运算“取反”&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;xml&amp;quot;&amp;gt;&lt;br /&gt;
mysql&amp;gt; SELECT 5 &amp;amp; ~1;&lt;br /&gt;
        -&amp;gt; 4&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
结果是一个无符号的 64 位整数。&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
当前，位函数和运算符需要“'''BIGINT'''”(64 位整数)个参数并返回“BIGINT”值，因此它们的'''最大范围为 64 位'''。其他类型的参数将转换为“BIGINT”，并且可能会发生截断。【？？？】&lt;br /&gt;
* MySQL 8.0 的扩展更改了这种强制转换为“BIGINT”的行为：位函数和运算符允许使用二进制字符串类型的参数（“BINARY”，“VARBINARY”和“BLOB”类型），使它们能够接受参数并产生大于 64 位的返回值。因此，在 MySQL 5.7 中对二进制参数的位操作可能在 MySQL 8.0 中产生不同的结果。为了提前通知这种潜在的行为变化，从 MySQL 5.7.11 开始，服务器会针对位操作生成警告，这些位操作的二进制参数未在 MySQL 8.0 中转换为整数。这些警告为重写受影响的语句提供了机会。为了显式地产生 MySQL 5.7 行为，使其在升级到 8.0 后不会改变，请转换位操作二进制参数以将其转换为整数。&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
需要注意的五种有问题的表达式类型是：&lt;br /&gt;
# “nonliteral_binary { &amp;amp; | ^ } binary”：&lt;br /&gt;
# “binary  { &amp;amp; | ^ } nonliteral_binary”：&lt;br /&gt;
# “nonliteral_binary { &amp;lt;&amp;lt; &amp;gt;&amp;gt; } anything”：&lt;br /&gt;
# “~ nonliteral_binary”：&lt;br /&gt;
# “AGGR_BIT_FUNC(nonliteral_binary)”：&lt;br /&gt;
'''这些表达式在 MySQL 5.7 中返回BIGINT，在 8.0 中返回二进制字符串。'''【？？？】&lt;br /&gt;
符号说明：&lt;br /&gt;
* “{ op1 op2 ... }”：适用于给定表达式类型的运算符列表。&lt;br /&gt;
* “binary *”：任何类型的二进制字符串参数，包括十六进制字符，位字符或 NULL字符。&lt;br /&gt;
* “nonliteral_binary *”：参数是二进制字符串值，而不是十六进制字符，位字符或 NULL字符。&lt;br /&gt;
* “AGGR_BIT_FUNC *”：带有位值参数的聚合函数：“BIT_AND()”，“BIT_OR()”，“BIT_XOR()”。&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
【？？？】&amp;lt;br/&amp;gt;&lt;br /&gt;
服务器为语句中的每个有问题的表达式生成一个警告，而不是为处理的每一行生成一个警告。假定包含两个有问题的表达式的语句从表中选择三行。每个语句执行的警告数量是两个，而不是六个。如下：&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;xml&amp;quot;&amp;gt;&lt;br /&gt;
mysql&amp;gt; CREATE TABLE t(vbin1 VARBINARY(32), vbin2 VARBINARY(32));&lt;br /&gt;
Query OK, 0 rows affected (0.03 sec)&lt;br /&gt;
&lt;br /&gt;
mysql&amp;gt; INSERT INTO t VALUES (3,1), (3,2), (3,3);&lt;br /&gt;
Query OK, 3 rows affected (0.01 sec)&lt;br /&gt;
Records: 3  Duplicates: 0  Warnings: 0&lt;br /&gt;
&lt;br /&gt;
mysql&amp;gt; SELECT HEX(vbin1 &amp;amp; vbin2) AS op1,&lt;br /&gt;
    -&amp;gt; HEX(vbin1 | vbin2) AS op2&lt;br /&gt;
    -&amp;gt; FROM t;&lt;br /&gt;
+------+------+&lt;br /&gt;
| op1  | op2  |&lt;br /&gt;
+------+------+&lt;br /&gt;
| 1    | 3    |&lt;br /&gt;
| 2    | 3    |&lt;br /&gt;
| 3    | 3    |&lt;br /&gt;
+------+------+&lt;br /&gt;
3 rows in set, 2 warnings (0.00 sec)&lt;br /&gt;
&lt;br /&gt;
mysql&amp;gt; SHOW WARNINGS\G&lt;br /&gt;
*************************** 1. row ***************************&lt;br /&gt;
  Level: Warning&lt;br /&gt;
   Code: 1287&lt;br /&gt;
Message: Bitwise operations on BINARY will change behavior in a future&lt;br /&gt;
         version, check the 'Bit functions' section in the manual.&lt;br /&gt;
*************************** 2. row ***************************&lt;br /&gt;
  Level: Warning&lt;br /&gt;
   Code: 1287&lt;br /&gt;
Message: Bitwise operations on BINARY will change behavior in a future&lt;br /&gt;
         version, check the 'Bit functions' section in the manual.&lt;br /&gt;
2 rows in set (0.00 sec)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
为了避免在升级到 MySQL 8.0 后受影响的语句产生不同的结果，请对其进行重写，以使其不产生位操作警告。为此，请使用“'''CAST(... AS UNSIGNED)'''”将至少一个二进制参数转换为'''BIGINT'''。这使得 MySQL 5.7 隐式二进制到整数强制转换是显式的：&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;xml&amp;quot;&amp;gt;&lt;br /&gt;
mysql&amp;gt; SELECT HEX(CAST(vbin1 AS UNSIGNED) &amp;amp; CAST(vbin2 AS UNSIGNED)) AS op1,&lt;br /&gt;
    -&amp;gt; HEX(CAST(vbin1 AS UNSIGNED) | CAST(vbin2 AS UNSIGNED)) AS op2&lt;br /&gt;
    -&amp;gt; FROM t;&lt;br /&gt;
+------+------+&lt;br /&gt;
| op1  | op2  |&lt;br /&gt;
+------+------+&lt;br /&gt;
| 1    | 3    |&lt;br /&gt;
| 2    | 3    |&lt;br /&gt;
| 3    | 3    |&lt;br /&gt;
+------+------+&lt;br /&gt;
3 rows in set (0.01 sec)&lt;br /&gt;
&lt;br /&gt;
mysql&amp;gt; SHOW WARNINGS\G&lt;br /&gt;
Empty set (0.00 sec)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
如下所示重写语句，MySQL 8.0 尊重将二进制参数视为整数的意图，并产生与 5.7 中相同的结果。同样，将语句从 MySQL 5.7 复制到 8.0 不会在不同服务器上产生不同的结果。&lt;br /&gt;
&lt;br /&gt;
不能重写的受影响的语句在升级和复制方面受到以下潜在问题的困扰：&lt;br /&gt;
# 升级到 MySQL 8.0 后，该语句可能返回不同的结果。&lt;br /&gt;
# 对于基于语句和混合格式的二进制日志记录，从旧版本复制到 MySQL 8.0 可能会失败。对于在 8.0 服务器上重放较旧的二进制日志(例如，使用mysqlbinlog)也是如此。为避免这种情况，请在较旧的主服务器上切换到基于行的二进制日志记录。&lt;/div&gt;</summary>
		<author><name>Eijux</name></author>
	</entry>
</feed>