12.8.3 Character Set and Collation of Function Results字符集与函数结果的排序

MySQL has many operators and functions that return a string. MySQL有许多返回字符串的操作符和函数。This section answers the question: What is the character set and collation of such a string?本节回答的问题是:这种字符串的字符集和排序规则是什么?

For simple functions that take string input and return a string result as output, the output's character set and collation are the same as those of the principal input value. 对于接受字符串输入并返回字符串结果作为输出的简单函数,输出的字符集和排序规则与主输入值的字符集和排序规则相同。For example, UPPER(X) returns a string with the same character string and collation as X. 例如,UPPER(X)返回与X具有相同字符串和排序规则的字符串。The same applies for INSTR(), LCASE(), LOWER(), LTRIM(), MID(), REPEAT(), REPLACE(), REVERSE(), RIGHT(), RPAD(), RTRIM(), SOUNDEX(), SUBSTRING(), TRIM(), UCASE(), and UPPER().这同样适用于INSTR()LCASE()LOWER()LTRIM()MID()REPEAT()REPLACE()REVERSE()RIGHT()RPAD()RTRIM()SOUNDEX()SUBSTRING()TRIM()UCASE()UPPER()

Note注意

The REPLACE() function, unlike all other functions, always ignores the collation of the string input and performs a case-sensitive comparison.与所有其他函数不同,REPLACE()函数总是忽略字符串输入的排序规则,并执行区分大小写的比较。

If a string input or function result is a binary string, the string has the binary character set and collation. 如果字符串输入或函数结果是二进制字符串,则该字符串具有binary字符集和排序规则。This can be checked by using the CHARSET() and COLLATION() functions, both of which return binary for a binary string argument:这可以通过使用CHARSET()COLLATION()函数来检查,这两个函数都为二进制字符串参数返回二进制:

mysql> SELECT CHARSET(BINARY 'a'), COLLATION(BINARY 'a');
+---------------------+-----------------------+
| CHARSET(BINARY 'a') | COLLATION(BINARY 'a') |
+---------------------+-----------------------+
| binary              | binary                |
+---------------------+-----------------------+

For operations that combine multiple string inputs and return a single string output, the aggregation rules of standard SQL apply for determining the collation of the result:对于组合多个字符串输入并返回单个字符串输出的操作,标准SQL的“聚合规则”适用于确定结果的排序规则:

For example, with CASE ... WHEN a THEN b WHEN b THEN c COLLATE X END, the resulting collation is X. 例如,用CASE ... WHEN a THEN b WHEN b THEN c COLLATE X END,得到的排序规则是XThe same applies for UNION, ||, CONCAT(), ELT(), GREATEST(), IF(), and LEAST().这同样适用于UNION||CONCAT()ELT()GREATEST()IF()LEAST()

For operations that convert to character data, the character set and collation of the strings that result from the operations are defined by the character_set_connection and collation_connection system variables that determine the default connection character set and collation (see Section 10.4, “Connection Character Sets and Collations”). 对于转换为字符数据的操作,操作产生的字符串的字符集和排序规则由character_set_connectioncollation_connection系统变量定义,这些变量确定默认的连接字符集和排序规则(请参阅第10.4节,“连接字符集和排序规则”)。This applies only to BIN_TO_UUID(), CAST(), CONV(), FORMAT(), HEX(), and SPACE().这仅适用于BIN_to_UUID()CAST()CONV()FORMAT()HEX()SPACE()

An exception to the preceding principle occurs for expressions for virtual generated columns. 对于虚拟生成列的表达式,会出现与上述原则不同的异常。In such expressions, the table character set is used for BIN_TO_UUID(), CONV(), or HEX() results, regardless of connection character set.在这种表达式中,表字符集用于BIN_TO_UUID()CONV()HEX()结果,而与连接字符集无关。

If there is any question about the character set or collation of the result returned by a string function, use the CHARSET() or COLLATION() function to find out:如果对字符串函数返回的结果的字符集或排序规则有任何疑问,请使用CHARSET()COLLATION()函数查找:

mysql> SELECT USER(), CHARSET(USER()), COLLATION(USER());
+----------------+-----------------+-------------------+
| USER()         | CHARSET(USER()) | COLLATION(USER()) |
+----------------+-----------------+-------------------+
| test@localhost | utf8            | utf8_general_ci   |
+----------------+-----------------+-------------------+
mysql> SELECT CHARSET(COMPRESS('abc')), COLLATION(COMPRESS('abc'));
+--------------------------+----------------------------+
| CHARSET(COMPRESS('abc')) | COLLATION(COMPRESS('abc')) |
+--------------------------+----------------------------+
| binary                   | binary                     |
+--------------------------+----------------------------+