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(
returns a string with the same character string and collation as X
)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()
。
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的“聚合规则”适用于确定结果的排序规则:
If an explicit 如果出现显式COLLATE
occurs, use Y
Y
.COLLATE
,则使用Y
Y
。
If explicit 如果出现显式COLLATE
and Y
COLLATE
occur, raise an error.Z
COLLATE
和Y
COLLATE
,则引发错误。Z
>Otherwise, if all collations are Y
, use Y
.>否则,如果所有排序规则都是Y
,则使用Y
。
Otherwise, the result has no collation.否则,结果没有排序规则。
For example, with 例如,用CASE ... WHEN a THEN b WHEN b THEN c COLLATE
, the resulting collation is X
ENDX
. CASE ... WHEN a THEN b WHEN b THEN c COLLATE
,得到的排序规则是X
ENDX
。The 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_connection
和collation_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 | +--------------------------+----------------------------+