9.1.1 String Literals字符串文字

A string is a sequence of bytes or characters, enclosed within either single quote (') or double quote (") characters. 字符串是字节或字符的序列,用单引号(')或双引号(")字符括起来。Examples:例如:

'a string'
"another string"

Quoted strings placed next to each other are concatenated to a single string. 相邻放置的带引号的字符串连接到单个字符串。The following lines are equivalent:以下行是等效的:

'a string'
'a' ' ' 'string'

If the ANSI_QUOTES SQL mode is enabled, string literals can be quoted only within single quotation marks because a string quoted within double quotation marks is interpreted as an identifier.如果启用了ANSI_QUOTES SQL模式,则只能在单引号内引用字符串文字,因为在双引号内引用的字符串将被解释为标识符。

A binary string is a string of bytes. 二进制字符串是字节字符串。Every binary string has a character set and collation named binary. 每个二进制字符串都有一个名为binary的字符集和排序规则。A nonbinary string is a string of characters. 非二进制字符串是字符串。It has a character set other than binary and a collation that is compatible with the character set.它有一个binary以外的字符集和一个与字符集兼容的排序规则。

For both types of strings, comparisons are based on the numeric values of the string unit. 对于这两种类型的字符串,比较都基于字符串单位的数值。For binary strings, the unit is the byte; comparisons use numeric byte values. 对于二进制字符串,单位是字节;比较使用数字字节值。For nonbinary strings, the unit is the character and some character sets support multibyte characters; comparisons use numeric character code values. 对于非二进制字符串,单位是字符,一些字符集支持多字节字符;比较使用数字字符代码值。Character code ordering is a function of the string collation. 字符代码排序是字符串排序的函数。(For more information, see Section 10.8.5, “The binary Collation Compared to _bin Collations”.)(有关更多信息,请参阅第10.8.5节,“二进制排序规则与_bin排序规则的比较”。)

Note注意

Within the mysql client, binary strings display using hexadecimal notation, depending on the value of the --binary-as-hex. mysql客户端中,二进制字符串使用十六进制表示法显示,具体取决于--binary-as-hex的值。For more information about that option, see Section 4.5.1, “mysql — The MySQL Command-Line Client”.有关该选项的更多信息,请参阅第4.5.1节,“mysql-mysql命令行客户端”

A character string 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:一个字符串文字可以有一个可选的字符集导入器和COLLATE子句,将其指定为使用特定字符集和排序规则的字符串:

[_charset_name]'string' [COLLATE collation_name]

Examples:例如:

SELECT _latin1'string';
SELECT _binary'string';
SELECT _utf8'string' COLLATE utf8_danish_ci;

You can use N'literal' (or n'literal') to create a string in the national character set. 可以使用N'literal'(或n'literal')在国家字符集中创建字符串。These statements are equivalent:这些语句相当于:

SELECT N'some text';
SELECT n'some text';
SELECT _utf8'some text';

For information about these forms of string syntax, see Section 10.3.7, “The National Character Set”, and Section 10.3.8, “Character Set Introducers”.有关这些形式的字符串语法的信息,请参阅第10.3.7节,“国家字符集”第10.3.8节,“字符集介绍人”

Within a string, certain sequences have special meaning unless the NO_BACKSLASH_ESCAPES SQL mode is enabled. 在字符串中,某些序列具有特殊意义,除非启用了NO_BACKSLASH_ESCAPESSQL模式。Each of these sequences begins with a backslash (\), known as the escape character. 每个序列都以反斜杠(\)开头,称为转义字符MySQL recognizes the escape sequences shown in Table 9.1, “Special Character Escape Sequences”. MySQL识别表9.1“特殊字符转义序列”中所示的转义序列。For all other escape sequences, backslash is ignored. 对于所有其他转义序列,将忽略反斜杠。That is, the escaped character is interpreted as if it was not escaped. 也就是说,转义字符被解释为未转义。For example, \x is just x. 例如,\x就是xThese sequences are case-sensitive. 这些序列区分大小写。For example, \b is interpreted as a backspace, but \B is interpreted as B. 例如,\b被解释为退格,但\B被解释为BEscape processing is done according to the character set indicated by the character_set_connection system variable. 转义处理根据character_set_connection系统变量指示的字符集完成。This is true even for strings that are preceded by an introducer that indicates a different character set, as discussed in Section 10.3.6, “Character String Literal Character Set and Collation”.即使是前面有表示不同字符集的介绍人的字符串也是如此,如第10.3.6节,“字符串文字字符集和排序规则”所述。

Table 9.1 Special Character Escape Sequences特殊字符转义序列

Escape Sequence转义序列Character Represented by Sequence由序列表示的字符
\0An ASCII NUL (X'00') character
\'A single quote (') character单引号(')字符
\"A double quote (") character双引号(")字符
\bA backspace character退格字符
\nA newline (linefeed) character换行符
\rA carriage return character回车符
\tA tab character制表符
\ZASCII 26 (Control+Z); see note following the tableASCII 26(Control+Z);见下表注释
\\A backslash (\) character反斜杠(\)字符
\%A % character; see note following the table一个%字符;见下表注释
\_A _ character; see note following the table一个_字符;见下表注释

The ASCII 26 character can be encoded as \Z to enable you to work around the problem that ASCII 26 stands for END-OF-FILE on Windows. ASCII 26字符可以编码为\Z,以便解决ASCII 26在Windows上代表文件结束的问题。ASCII 26 within a file causes problems if you try to use mysql db_name < file_name.如果试图使用mysql db_name<file_name,文件中的ASCII 26会导致问题。

The \% and \_ sequences are used to search for literal instances of % and _ in pattern-matching contexts where they would otherwise be interpreted as wildcard characters. \%\_序列用于在模式匹配上下文中搜索%_的文本实例,否则它们将被解释为通配符。See the description of the LIKE operator in Section 12.8.1, “String Comparison Functions and Operators”. 请参阅第12.8.1节,“字符串比较函数和运算符”中对LIKE运算符的描述。If you use \% or \_ outside of pattern-matching contexts, they evaluate to the strings \% and \_, not to % and _.如果在模式匹配上下文之外使用\%\_\,则它们的计算结果为字符串\%\_,而不是%_

There are several ways to include quote characters within a string:有几种方法可以在字符串中包含引号字符:

The following SELECT statements demonstrate how quoting and escaping work:以下SELECT语句演示了引用和转义的工作原理:

mysql> SELECT 'hello', '"hello"', '""hello""', 'hel''lo', '\'hello';
+-------+---------+-----------+--------+--------+
| hello | "hello" | ""hello"" | hel'lo | 'hello |
+-------+---------+-----------+--------+--------+

mysql> SELECT "hello", "'hello'", "''hello''", "hel""lo", "\"hello";
+-------+---------+-----------+--------+--------+
| hello | 'hello' | ''hello'' | hel"lo | "hello |
+-------+---------+-----------+--------+--------+

mysql> SELECT 'This\nIs\nFour\nLines';
+--------------------+
| This
Is
Four
Lines |
+--------------------+

mysql> SELECT 'disappearing\ backslash';
+------------------------+
| disappearing backslash |
+------------------------+

To insert binary data into a string column (such as a BLOB column), you should represent certain characters by escape sequences. 要将二进制数据插入字符串列(例如BLOB列),应该通过转义序列来表示某些字符。Backslash (\) and the quote character used to quote the string must be escaped. 必须转义反斜杠(\)和用于引用字符串的引号字符。In certain client environments, it may also be necessary to escape NUL or Control+Z. 在某些客户端环境中,可能还需要退出NUL或Control+Z。The mysql client truncates quoted strings containing NUL characters if they are not escaped, and Control+Z may be taken for END-OF-FILE on Windows if not escaped. 如果未转义,mysql客户端会截断包含NUL字符的带引号字符串,如果未转义,Windows上的文件结尾可能会使用Control+Z。For the escape sequences that represent each of these characters, see Table 9.1, “Special Character Escape Sequences”.对于代表这些字符的转义序列,请参阅表9.1“特殊字符转义序列”

When writing application programs, any string that might contain any of these special characters must be properly escaped before the string is used as a data value in an SQL statement that is sent to the MySQL server. 在编写应用程序时,任何可能包含这些特殊字符的字符串都必须正确转义,然后才能在发送到MySQL服务器的SQL语句中将该字符串用作数据值。You can do this in two ways:您可以通过两种方式实现这一点: