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排序规则的比较”。)
Within the mysql client, binary strings display using hexadecimal notation, depending on the value of the 在mysql客户端中,二进制字符串使用十六进制表示法显示,具体取决于--binary-as-hex
. --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
' [COLLATEcollation_name
]
Examples:例如:
SELECT _latin1'string
'; SELECT _binary'string
'; SELECT _utf8'string
' COLLATE utf8_danish_ci;
You can use 可以使用N'
(or literal
'n'
) to create a string in the national character set. literal
'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_ESCAPES
SQL模式。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
就是x
。These sequences are case-sensitive. 这些序列区分大小写。For example, 例如,\b
is interpreted as a backspace, but \B
is interpreted as B
. \b
被解释为退格,但\B
被解释为B
。Escape 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特殊字符转义序列
\0 | An ASCII NUL (X'00' ) character |
\' | ' ) character' )字符 |
\" | " ) character" )字符 |
\b | |
\n | |
\r | |
\t | |
\Z | |
\\ | \ ) character\ )字符 |
\% | % character; see note following the table% 字符;见下表注释 |
\_ | _ character; see note following the table_ 字符;见下表注释 |
The ASCII 26 character can be encoded as ASCII 26字符可以编码为\Z
to enable you to work around the problem that ASCII 26 stands for END-OF-FILE on Windows. \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 请参阅第12.8.1节,“字符串比较函数和运算符”中对LIKE
operator in Section 12.8.1, “String Comparison Functions and Operators”. 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:有几种方法可以在字符串中包含引号字符:
A 在一个用'
inside a string quoted with '
may be written as ''
.'
引括起来的字符串中,一个'
可以写为''
。
A 在一个用"
inside a string quoted with "
may be written as ""
."
引括起来的字符串中,一个"
可以写为""
。
Precede the quote character by an escape character (在引号前面加一个转义字符(\
).\
)。
A 在一个用'
inside a string quoted with "
needs no special treatment and need not be doubled or escaped. "
引括起来的字符串中,一个'
不需要特殊处理,也不需要加倍或转义。In the same way, 同样地,在一个用"
inside a string quoted with '
needs no special treatment."
引括起来的字符串中,'
也不需要特殊处理。
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:您可以通过两种方式实现这一点:
Process the string with a function that escapes the special characters. 使用转义特殊字符的函数处理字符串。In a C program, you can use the 在C程序中,可以使用mysql_real_escape_string_quote()
C API function to escape characters. mysql_real_escape_string_quote()
C API函数来转义字符。See mysql_real_escape_string_quote(). 请参阅mysql_real_escape_string_quote()
。Within SQL statements that construct other SQL statements, you can use the 在构造其他SQL语句的SQL语句中,可以使用QUOTE()
function. QUOTE()
函数。The Perl DBI interface provides a Perl DBI接口提供了一种quote
method to convert special characters to the proper escape sequences. quote
方法,可以将特殊字符转换为正确的转义序列。See Section 29.9, “MySQL Perl API”. 请参阅第29.9节,“MySQL Perl API”。Other language interfaces may provide a similar capability.其他语言接口可能提供类似的功能。
As an alternative to explicitly escaping special characters, many MySQL APIs provide a placeholder capability that enables you to insert special markers into a statement string, and then bind data values to them when you issue the statement. 作为显式转义特殊字符的替代方法,许多MySQL API提供了占位符功能,使您能够在语句字符串中插入特殊标记,然后在发出语句时将数据值绑定到它们。In this case, the API takes care of escaping special characters in the values for you.在本例中,API负责为您转义值中的特殊字符。