<?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%E8%BD%AC%E6%8D%A2%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%E8%BD%AC%E6%8D%A2%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%E8%BD%AC%E6%8D%A2%E5%87%BD%E6%95%B0%E5%92%8C%E8%BF%90%E7%AE%97%E7%AC%A6&amp;action=history"/>
	<updated>2026-04-12T13:38:33Z</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%E8%BD%AC%E6%8D%A2%E5%87%BD%E6%95%B0%E5%92%8C%E8%BF%90%E7%AE%97%E7%AC%A6&amp;diff=6527&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%E8%BD%AC%E6%8D%A2%E5%87%BD%E6%95%B0%E5%92%8C%E8%BF%90%E7%AE%97%E7%AC%A6&amp;diff=6527&amp;oldid=prev"/>
		<updated>2023-03-31T15:09:37Z</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%E8%BD%AC%E6%8D%A2%E5%87%BD%E6%95%B0%E5%92%8C%E8%BF%90%E7%AE%97%E7%AC%A6&amp;diff=2230&amp;oldid=prev</id>
		<title>Eijux：​建立内容为“category:MySQL   强制转换函数和运算符： {| class=&quot;wikitable&quot; ! Name !! Description  |- | '''BINARY''' || 将字符串转换为二进制字符串 |-…”的新页面</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%E8%BD%AC%E6%8D%A2%E5%87%BD%E6%95%B0%E5%92%8C%E8%BF%90%E7%AE%97%E7%AC%A6&amp;diff=2230&amp;oldid=prev"/>
		<updated>2021-03-31T13:04:26Z</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;#039;&amp;#039;&amp;#039;BINARY&amp;#039;&amp;#039;&amp;#039; || 将字符串转换为二进制字符串 |-…”的新页面&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;
| '''BINARY''' || 将字符串转换为二进制字符串&lt;br /&gt;
|-&lt;br /&gt;
| '''CAST()''' || 将值强制转换为特定类型【用法：“'''CONVERT(expr USING transcoding_name)'''”或“'''CONVERT(expr, type)'''”】&lt;br /&gt;
|-&lt;br /&gt;
| '''CONVERT()''' || 将值强制转换为特定类型【用法：“'''CAST(expr AS type)'''”】&lt;br /&gt;
|}&lt;br /&gt;
* 强制转换函数和运算符可将值从一种数据类型转换为另一种数据类型。&lt;br /&gt;
* 如果使用“BINARY”，“CAST()”或“CONVERT()”转换索引列，则 MySQL 可能无法有效使用索引。&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;xml&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
带有“'''USING'''”子句的“'''CONVERT()'''”在不同字符集之间转换数据：&lt;br /&gt;
'''&amp;lt;syntaxhighlight lang=&amp;quot;xml&amp;quot;&amp;gt;&lt;br /&gt;
CONVERT(expr USING transcoding_name)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;'''&lt;br /&gt;
* 在 MySQL 中，转码名称与相应的字符集名称相同。如：&lt;br /&gt;
*: &amp;lt;syntaxhighlight lang=&amp;quot;xml&amp;quot;&amp;gt;&lt;br /&gt;
SELECT CONVERT('test' USING utf8);&lt;br /&gt;
SELECT CONVERT(_latin1'Müller' USING utf8);&lt;br /&gt;
INSERT INTO utf8_table (utf8_column)&lt;br /&gt;
    SELECT CONVERT(latin1_column USING utf8) FROM latin1_table;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
要在不同的字符集之间转换字符串，还可以使用（不带“USING”的）“'''CONVERT(expr, type)'''”语法或“'''CAST(expr AS type)'''”：【二者等效】&lt;br /&gt;
'''&amp;lt;syntaxhighlight lang=&amp;quot;xml&amp;quot;&amp;gt;&lt;br /&gt;
CONVERT(string, CHAR[(N)] CHARACTER SET charset_name)&lt;br /&gt;
CAST(string AS CHAR[(N)] CHARACTER SET charset_name)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;'''&lt;br /&gt;
如：&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;xml&amp;quot;&amp;gt;&lt;br /&gt;
SELECT CONVERT('test', CHAR CHARACTER SET utf8);&lt;br /&gt;
SELECT CAST('test' AS CHAR CHARACTER SET utf8);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
“CONVERT()”或“CAST()”调用中'''不允许使用“COLLATE”子句，但可以将其应用于函数结果'''。&lt;br /&gt;
# 例如，这些是合法的：&lt;br /&gt;
#: '''&amp;lt;syntaxhighlight lang=&amp;quot;xml&amp;quot;&amp;gt;&lt;br /&gt;
SELECT CONVERT('test' USING utf8) COLLATE utf8_bin;&lt;br /&gt;
SELECT CONVERT('test', CHAR CHARACTER SET utf8) COLLATE utf8_bin;&lt;br /&gt;
SELECT CAST('test' AS CHAR CHARACTER SET utf8) COLLATE utf8_bin;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;'''&lt;br /&gt;
# 但是这些是非法的：&lt;br /&gt;
#: &amp;lt;syntaxhighlight lang=&amp;quot;xml&amp;quot;&amp;gt;&lt;br /&gt;
SELECT CONVERT('test' USING utf8 COLLATE utf8_bin);&lt;br /&gt;
SELECT CONVERT('test', CHAR CHARACTER SET utf8 COLLATE utf8_bin);&lt;br /&gt;
SELECT CAST('test' AS CHAR CHARACTER SET utf8 COLLATE utf8_bin);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
通常，不能以不区分大小写的方式比较“'''BLOB'''”值或其他二进制字符串，因为二进制字符串使用“'''binary'''”字符集，该字符集与字母大小写的概念没有任何关联。要执行不区分大小写的比较，请首先使用“CONVERT()”或“CAST()”函数将值转换为非二进制字符串。结果字符串的比较使用其排序规则。【如，中文拼音排序】&lt;br /&gt;
: 例如，如果转换结果字符集具有不区分大小写的排序规则，则“LIKE”操作不区分大小写。对于以下操作，这是正确的，因为默认的“latin1”排序规则（“latin1_swedish_ci”）不区分大小写：&lt;br /&gt;
: &amp;lt;syntaxhighlight lang=&amp;quot;xml&amp;quot;&amp;gt;&lt;br /&gt;
SELECT 'A' LIKE CONVERT(blob_col USING latin1)&lt;br /&gt;
  FROM tbl_name;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
: 要为转换后的字符串指定特定的排序规则，请在“CONVERT()”调用之后使用“COLLATE”子句：&lt;br /&gt;
: &amp;lt;syntaxhighlight lang=&amp;quot;xml&amp;quot;&amp;gt;&lt;br /&gt;
SELECT 'A' LIKE CONVERT(blob_col USING latin1) COLLATE latin1_german1_ci&lt;br /&gt;
  FROM tbl_name;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
若要使用其他字符集，请在前面的语句中用其名称替换latin1(并类似地使用其他排序规则)。&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
字符集转换在二进制字符串的字母大写转换之前也很有用。直接应用于二进制字符串时，LOWER() 和 UPPER() 无效，因为字母大小写的概念不适用。要执行二进制字符串的字母大小写转换，请首先使用适合于存储在字符串中的数据的字符集将其转换为非二进制字符串：&lt;br /&gt;
: &amp;lt;syntaxhighlight lang=&amp;quot;xml&amp;quot;&amp;gt;&lt;br /&gt;
mysql&amp;gt; SET @str = BINARY 'New York';&lt;br /&gt;
mysql&amp;gt; SELECT LOWER(@str), LOWER(CONVERT(@str USING latin1));&lt;br /&gt;
+-------------+-----------------------------------+&lt;br /&gt;
| LOWER(@str) | LOWER(CONVERT(@str USING latin1)) |&lt;br /&gt;
+-------------+-----------------------------------+&lt;br /&gt;
| New York    | new york                          |&lt;br /&gt;
+-------------+-----------------------------------+&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
请注意，如果使用 BINARY，CAST() 或 CONVERT() 转换索引列，则 MySQL 可能无法有效使用索引。&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
对于时间值，几乎不需要使用“CAST()”来提取不同格式的数据。而是使用“'''EXTRACT()'''”，“'''DATE_FORMAT()'''”或“'''TIME_FORMAT()'''”之类的函数。&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
MySQL 支持带符号和无符号 64 位值的算术运算。对于其中一个操作数是无符号整数的数字运算符(例如+或-)，默认情况下结果是无符号的(请参见第 12.5.1 节“算术运算符”)。要覆盖此内容，请使用“'''SIGNED'''”或“'''UNSIGNED'''”强制转换运算符将值分别强制转换为有符号或无符号 64 位整数。&lt;br /&gt;
: &amp;lt;syntaxhighlight lang=&amp;quot;xml&amp;quot;&amp;gt;&lt;br /&gt;
mysql&amp;gt; SELECT 1 - 2;&lt;br /&gt;
        -&amp;gt; -1&lt;br /&gt;
mysql&amp;gt; SELECT CAST(1 - 2 AS UNSIGNED);&lt;br /&gt;
        -&amp;gt; 18446744073709551615&lt;br /&gt;
mysql&amp;gt; SELECT CAST(CAST(1 - 2 AS UNSIGNED) AS SIGNED);&lt;br /&gt;
        -&amp;gt; -1&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
如果任何一个操作数都是浮点值，则结果是浮点值，并且不受前面的规则的影响。 (在这种情况下，“'''DECIMAL'''”列值被视为浮点值。)&lt;br /&gt;
: &amp;lt;syntaxhighlight lang=&amp;quot;xml&amp;quot;&amp;gt;&lt;br /&gt;
mysql&amp;gt; SELECT CAST(1 AS UNSIGNED) - 2.0;&lt;br /&gt;
        -&amp;gt; -1.0&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
SQL 模式会影响转换操作的结果。例子：&lt;br /&gt;
# 为了将“零”日期字符串转换为日期，“CONVERT()”和“CAST()”返回“NULL”并在启用“'''NO_ZERO_DATE'''” SQL 模式时产生警告。&lt;br /&gt;
# 对于整数减法，如果启用了“'''NO_UNSIGNED_SUBTRACTION'''” SQL 模式，则即使任何操作数都为无符号数，减法结果都是有符号数。&lt;/div&gt;</summary>
		<author><name>Eijux</name></author>
	</entry>
</feed>