Table 12.12 String Functions and Operators字符串函数和运算符
ASCII() | |
BIN() | |
BIT_LENGTH() | |
CHAR() | |
CHAR_LENGTH() | |
CHARACTER_LENGTH() | CHAR_LENGTH() 的同义词 |
CONCAT() | |
CONCAT_WS() | |
ELT() | |
EXPORT_SET() | |
FIELD() | |
FIND_IN_SET() | |
FORMAT() | |
FROM_BASE64() | |
HEX() | |
INSERT() | |
INSTR() | |
LCASE() | LOWER() 的同义词 |
LEFT() | |
LENGTH() | |
LIKE | |
LOAD_FILE() | |
LOCATE() | |
LOWER() | |
LPAD() | |
LTRIM() | |
MAKE_SET() | |
MATCH | |
MID() | |
NOT LIKE | |
NOT REGEXP | |
OCT() | |
OCTET_LENGTH() | LENGTH() 的同义词 |
ORD() | |
POSITION() | LOCATE() 的同义词 |
QUOTE() | |
REGEXP | |
REGEXP_INSTR() | |
REGEXP_LIKE() | |
REGEXP_REPLACE() | |
REGEXP_SUBSTR() | |
REPEAT() | |
REPLACE() | |
REVERSE() | |
RIGHT() | |
RLIKE | |
RPAD() | |
RTRIM() | |
SOUNDEX() | |
SOUNDS LIKE | |
SPACE() | |
STRCMP() | |
SUBSTR() | |
SUBSTRING() | |
SUBSTRING_INDEX() | |
TO_BASE64() | |
TRIM() | |
UCASE() | UPPER() 的同义词 |
UNHEX() | |
UPPER() | |
WEIGHT_STRING() |
String-valued functions return 如果结果的长度大于NULL
if the length of the result would be greater than the value of the max_allowed_packet
system variable. max_allowed_packet
系统变量的值,则字符串值函数返回NULL
。See Section 5.1.1, “Configuring the Server”.请参阅第5.1.1节“配置服务器”。
For functions that operate on string positions, the first position is numbered 1.对于操作字符串位置的函数,第一个位置编号为1。
For functions that take length arguments, noninteger arguments are rounded to the nearest integer.对于采用长度参数的函数,非整数参数将舍入到最接近的整数。
Returns the numeric value of the leftmost character of the string 返回字符串str
. str
最左边的字符的数值。Returns 如果0
if str
is the empty string. str
是空字符串,则返回0
。Returns 如果NULL
if str
is NULL
. str
为NULL
,则返回NULL
。ASCII()
works for 8-bit characters.ASCII()
适用于8位字符。
mysql>SELECT ASCII('2');
-> 50 mysql>SELECT ASCII(2);
-> 50 mysql>SELECT ASCII('dx');
-> 100
Returns a string representation of the binary value of 返回二进制值N
, where N
is a longlong (BIGINT
) number. N
的字符串表示形式,其中N
是longlong(BIGINT
)数。This is equivalent to 这相当于CONV(
. N
,10,2)CONV(
。N
,10,2)Returns 如果NULL
if N
is NULL
.N
为NULL
,则返回NULL
。
mysql> SELECT BIN(12);
-> '1100'
Returns the length of the string 返回字符串str
in bits.str
的长度(位)。
mysql> SELECT BIT_LENGTH('text');
-> 32
CHAR(
N
,... [USING charset_name
])
CHAR()
interprets each argument N
as an integer and returns a string consisting of the characters given by the code values of those integers. NULL
values are skipped.CHAR()
将每个参数N
解释为一个整数,并返回由这些整数的代码值给定的字符组成的字符串。跳过空值。
mysql>SELECT CHAR(77,121,83,81,'76');
-> 'MySQL' mysql>SELECT CHAR(77,77.3,'77.3');
-> 'MMM'
大于255的CHAR()
arguments larger than 255 are converted into multiple result bytes. CHAR()
参数将转换为多个结果字节。For example, 例如,CHAR(256)
is equivalent to CHAR(1,0)
, and CHAR(256*256)
is equivalent to CHAR(1,0,0)
:CHAR(256)
等价于CHAR(1,0)
,CHAR(256*256)
等价于CHAR(1,0,0)
:
mysql>SELECT HEX(CHAR(1,0)), HEX(CHAR(256));
+----------------+----------------+ | HEX(CHAR(1,0)) | HEX(CHAR(256)) | +----------------+----------------+ | 0100 | 0100 | +----------------+----------------+ mysql>SELECT HEX(CHAR(1,0,0)), HEX(CHAR(256*256));
+------------------+--------------------+ | HEX(CHAR(1,0,0)) | HEX(CHAR(256*256)) | +------------------+--------------------+ | 010000 | 010000 | +------------------+--------------------+
By default, 默认情况下,CHAR()
returns a binary string. CHAR()
返回一个二进制字符串。To produce a string in a given character set, use the optional 要在给定的字符集中生成字符串,请使用可选的USING
clause:USING
子句:
mysql> SELECT CHARSET(CHAR(X'65')), CHARSET(CHAR(X'65' USING utf8));
+----------------------+---------------------------------+
| CHARSET(CHAR(X'65')) | CHARSET(CHAR(X'65' USING utf8)) |
+----------------------+---------------------------------+
| binary | utf8 |
+----------------------+---------------------------------+
If 如果给定USING
is given and the result string is illegal for the given character set, a warning is issued. USING
并且结果字符串对于给定的字符集是非法的,则发出警告。Also, if strict SQL mode is enabled, the result from 此外,如果启用了严格SQL模式,CHAR()
becomes NULL
.CHAR()
的结果将变为NULL
。
If 如果从mysql客户机中调用CHAR()
is invoked from within the mysql client, binary strings display using hexadecimal notation, depending on the value of the --binary-as-hex
. CHAR()
,则二进制字符串将使用十六进制表示法显示,具体取决于--binary-as-hex
的值。For more information about that option, see Section 4.5.1, “mysql — The MySQL Command-Line Client”.有关该选项的更多信息,请参阅第4.5.1节,“mysql命令行客户端”。
Returns the length of the string 返回字符串str
, measured in characters. str
的长度,以字符为单位。A multibyte character counts as a single character. 多字节字符计为单个字符。This means that for a string containing five 2-byte characters, 这意味着,对于包含五个2字节字符的字符串,LENGTH()
returns 10
, whereas CHAR_LENGTH()
returns 5
.LENGTH()
返回10
,而CHAR_LENGTH()
返回5
。
CHARACTER_LENGTH()
is a synonym for CHAR_LENGTH()
.CHARACTER_LENGTH()
是CHAR_LENGTH()
的同义词。
Returns the string that results from concatenating the arguments. May have one or more arguments. 返回连接参数所产生的字符串。可能有一个或多个参数。If all arguments are nonbinary strings, the result is a nonbinary string. 如果所有参数都是非二进制字符串,则结果是非二进制字符串。If the arguments include any binary strings, the result is a binary string. 如果参数包含任何二进制字符串,则结果是二进制字符串。A numeric argument is converted to its equivalent nonbinary string form.数值参数将转换为其等效的非二进制字符串形式。
如果任一参数为CONCAT()
returns NULL
if any argument is NULL
.NULL
,CONCAT()
将返回NULL
。
mysql>SELECT CONCAT('My', 'S', 'QL');
-> 'MySQL' mysql>SELECT CONCAT('My', NULL, 'QL');
-> NULL mysql>SELECT CONCAT(14.3);
-> '14.3'
For quoted strings, concatenation can be performed by placing the strings next to each other:对于带引号的字符串,可以通过将字符串相邻放置来执行串联:
mysql> SELECT 'My' 'S' 'QL';
-> 'MySQL'
If 如果从mysql客户机中调用CONCAT()
is invoked from within the mysql client, binary string results display using hexadecimal notation, depending on the value of the --binary-as-hex
. CONCAT()
,则二进制字符串结果将使用十六进制表示法显示,具体取决于--binary-as-hex
的值。For more information about that option, see Section 4.5.1, “mysql — The MySQL Command-Line Client”.有关该选项的更多信息,请参阅第4.5.1节,“mysql命令行客户端”。
CONCAT_WS(
separator
,str1
,str2
,...)
CONCAT_WS()
stands for Concatenate With Separator and is a special form of CONCAT()
. CONCAT_WS()
表示用分隔符连接,是CONCAT()
的一种特殊形式。The first argument is the separator for the rest of the arguments. 第一个参数是其余参数的分隔符。The separator is added between the strings to be concatenated. 分隔符添加在要连接的字符串之间。The separator can be a string, as can the rest of the arguments. 分隔符可以是字符串,其他参数也可以是字符串。If the separator is 如果分隔符为NULL
, the result is NULL
.NULL
,则结果为NULL
。
mysql>SELECT CONCAT_WS(',','First name','Second name','Last Name');
-> 'First name,Second name,Last Name' mysql>SELECT CONCAT_WS(',','First name',NULL,'Last Name');
-> 'First name,Last Name'
CONCAT_WS()
does not skip empty strings. 不跳过空字符串。However, it does skip any 但是,它会跳过分隔符参数之后的任何NULL
values after the separator argument.NULL
值。
ELT()
returns the N
th element of the list of strings: str1
if N
= 1
, str2
if N
= 2
, and so on. ELT()
返回字符串列表的第N
个元素:如果N
= 1
,则返回str1
;如果N
= 2
,则返回str2
,依此类推。Returns 如果NULL
if N
is less than 1
or greater than the number of arguments. N
小于1或大于参数数目,则返回NULL
。ELT()
is the complement of FIELD()
.ELT()
是FIELD()
的补码。
mysql>SELECT ELT(1, 'Aa', 'Bb', 'Cc', 'Dd');
-> 'Aa' mysql>SELECT ELT(4, 'Aa', 'Bb', 'Cc', 'Dd');
-> 'Dd'
EXPORT_SET(
bits
,on
,off
[,separator
[,number_of_bits
]])
Returns a string such that for every bit set in the value 返回一个字符串,这样对于值位中设置的每一位,您都会得到一个bits
, you get an on
string and for every bit not set in the value, you get an off
string. on
字符串,对于值中未设置的每一位,您都会得到一个off
字符串。Bits in 从右到左(从低位到高位)检查bits
are examined from right to left (from low-order to high-order bits). bits
中的位。Strings are added to the result from left to right, separated by the 字符串从左到右添加到结果中,由separator
string (the default being the comma character ,
). separator
字符串分隔(默认为逗号字符,
)。The number of bits examined is given by 检查的位数由number_of_bits
, which has a default of 64 if not specified. number_of_bits
给出,如果未指定,则默认值为64。如果大于64,则number_of_bits
is silently clipped to 64 if larger than 64. number_of_bits
将自动剪裁为64。It is treated as an unsigned integer, so a value of −1 is effectively the same as 64.它被视为无符号整数,因此值-1实际上与64相同。
mysql>SELECT EXPORT_SET(5,'Y','N',',',4);
-> 'Y,N,Y,N' mysql>SELECT EXPORT_SET(6,'1','0',',',10);
-> '0,1,1,0,0,0,0,0,0,0'
Returns the index (position) of 返回str
in the str1
, str2
, str3
, ...
list. str
在str1
、str2
、str3
……列表中的索引(位置)。Returns 如果找不到0
if str
is not found.str
,则返回0
。
If all arguments to 如果FIELD()
are strings, all arguments are compared as strings. FIELD()
的所有参数都是字符串,则将所有参数作为字符串进行比较。If all arguments are numbers, they are compared as numbers. 如果所有参数都是数字,则它们将作为数字进行比较。Otherwise, the arguments are compared as double.否则,参数将作为双参数进行比较。
If 如果str
is NULL
, the return value is 0
because NULL
fails equality comparison with any value. str
为NULL
,则返回值为0
,因为NULL
无法与任何值进行相等比较。FIELD()
is the complement of ELT()
.FIELD()
是ELT()
的补码。
mysql>SELECT FIELD('Bb', 'Aa', 'Bb', 'Cc', 'Dd', 'Ff');
-> 2 mysql>SELECT FIELD('Gg', 'Aa', 'Bb', 'Cc', 'Dd', 'Ff');
-> 0
Returns a value in the range of 1 to 如果字符串N
if the string str
is in the string list strlist
consisting of N
substrings. str
位于由N
个子字符串组成的字符串列表strlist
中,则返回1到N
之间的值。A string list is a string composed of substrings separated by 字符串列表是一个由,
characters. ,
字符分隔的子字符串组成的字符串。If the first argument is a constant string and the second is a column of type 如果第一个参数是常量字符串,第二个参数是SET
, the FIND_IN_SET()
function is optimized to use bit arithmetic. SET
类型的列,则FIND_IN_SET()
函数将优化为使用位算术。Returns 如果0
if str
is not in strlist
or if strlist
is the empty string. str
不在strlist
中,或strlist
是空字符串,则返回0。Returns 如果任一参数为NULL
if either argument is NULL
. NULL
,则返回NULL
。This function does not work properly if the first argument contains a comma (如果第一个参数包含逗号(,
) character.,
)字符,则此函数无法正常工作。
mysql> SELECT FIND_IN_SET('b','a,b,c,d');
-> 2
Formats the number 将数字X
to a format like '#,###,###.##'
, rounded to D
decimal places, and returns the result as a string. X
格式化为'#,###,###.##'
等格式,四舍五入到D
位小数,并以字符串形式返回结果。If 如果D
is 0
, the result has no decimal point or fractional part.D
为0
,则结果没有小数点或小数部分。
The optional third parameter enables a locale to be specified to be used for the result number's decimal point, thousands separator, and grouping between separators. 可选的第三个参数允许指定区域设置,以用于结果数字的小数点、千位分隔符和分隔符之间的分组。Permissible locale values are the same as the legal values for the 允许的区域设置值与lc_time_names
system variable (see Section 10.16, “MySQL Server Locale Support”). lc_time_names
系统变量的合法值相同(请参阅第10.16节,“MySQL服务器区域设置支持”)。If no locale is specified, the default is 如果未指定区域设置,则默认值为'en_US'
.'en_US'
。
mysql>SELECT FORMAT(12332.123456, 4);
-> '12,332.1235' mysql>SELECT FORMAT(12332.1,4);
-> '12,332.1000' mysql>SELECT FORMAT(12332.2,0);
-> '12,332' mysql>SELECT FORMAT(12332.2,2,'de_DE');
-> '12.332,20'
Takes a string encoded with the base-64 encoded rules used by 获取用TO_BASE64()
and returns the decoded result as a binary string. TO_BASE64()
使用的base-64编码规则编码的字符串,并将解码结果作为二进制字符串返回。The result is 如果参数为NULL
if the argument is NULL
or not a valid base-64 string. NULL
或不是有效的base-64字符串,则结果为NULL
。See the description of 有关编码和解码规则的详细信息,请参阅TO_BASE64()
for details about the encoding and decoding rules.TO_BASE64()
的说明。
mysql> SELECT TO_BASE64('abc'), FROM_BASE64(TO_BASE64('abc'));
-> 'JWJj', 'abc'
If 如果从mysql客户机中调用FROM_BASE64()
is invoked from within the mysql client, binary strings display using hexadecimal notation, depending on the value of the --binary-as-hex
. FROM_BASE64()
,则二进制字符串将使用十六进制表示法显示,具体取决于--binary-as-hex
的值。For more information about that option, see Section 4.5.1, “mysql — The MySQL Command-Line Client”.有关该选项的更多信息,请参阅第4.5.1节,“mysql命令行客户端”。
For a string argument 对于字符串参数str
, HEX()
returns a hexadecimal string representation of str
where each byte of each character in str
is converted to two hexadecimal digits. str
,HEX()
返回str
的十六进制字符串表示形式,其中str
中每个字符的每个字节都转换为两个十六进制数字。(Multibyte characters therefore become more than two digits.) (因此,多字节字符会变成两位数以上。)The inverse of this operation is performed by the 此操作的逆操作由UNHEX()
function.UNHEX()
函数执行。
For a numeric argument 对于数值参数N
, HEX()
returns a hexadecimal string representation of the value of N
treated as a longlong (BIGINT
) number. N
,HEX()
返回N
值的十六进制字符串表示形式,N
值被视为longlong(BIGINT
)数字。This is equivalent to 这相当于CONV(
. N
,10,16)CONV(
。N
,10,16)The inverse of this operation is performed by 此操作的逆操作由CONV(HEX(
.N
),16,10)CONV(HEX(
执行。N
),16,10)
mysql>SELECT X'616263', HEX('abc'), UNHEX(HEX('abc'));
-> 'abc', 616263, 'abc' mysql>SELECT HEX(255), CONV(HEX(255),16,10);
-> 'FF', 255
Returns the string 返回字符串str
, with the substring beginning at position pos
and len
characters long replaced by the string newstr
. str
,子字符串从位置pos
开始,len
个字符由字符串newstr
替换。Returns the original string if 如果pos
is not within the length of the string. pos
不在字符串长度内,则返回原始字符串。Replaces the rest of the string from position 如果pos
if len
is not within the length of the rest of the string. len
不在字符串其余部分的长度内,则从位置pos
替换字符串其余部分。Returns 如果任一参数为NULL
if any argument is NULL
.NULL
,则返回NULL
。
mysql>SELECT INSERT('Quadratic', 3, 4, 'What');
-> 'QuWhattic' mysql>SELECT INSERT('Quadratic', -1, 4, 'What');
-> 'Quadratic' mysql>SELECT INSERT('Quadratic', 3, 100, 'What');
-> 'QuWhat'
This function is multibyte safe.此函数是多字节安全的。
Returns the position of the first occurrence of substring 返回字符串substr
in string str
. str
中第一个出现的子字符串substr
的位置。This is the same as the two-argument form of 这与LOCATE()
, except that the order of the arguments is reversed.LOCATE()
的双参数形式相同,只是参数的顺序相反。
mysql>SELECT INSTR('foobarbar', 'bar');
-> 4 mysql>SELECT INSTR('xbar', 'foobar');
-> 0
This function is multibyte safe, and is case-sensitive only if at least one argument is a binary string.此函数是多字节安全的,并且仅当至少有一个参数是二进制字符串时才区分大小写。
LCASE()
is a synonym for LOWER()
.LCASE()
是LOWER()
的同义词。
在存储视图定义时,视图中使用的LCASE()
used in a view is rewritten as LOWER()
when storing the view's definition. LCASE()
被重写为LOWER()
。(Bug #12844279)
Returns the leftmost 返回字符串len
characters from the string str
, or NULL
if any argument is NULL
.str
中最左边的len
字符,如果任何参数为NULL
,则返回NULL
。
mysql> SELECT LEFT('foobarbar', 5);
-> 'fooba'
This function is multibyte safe.此函数是多字节安全的。
Returns the length of the string 返回字符串str
, measured in bytes. str
的长度,以字节为单位。A multibyte character counts as multiple bytes. 多字节字符计为多字节。This means that for a string containing five 2-byte characters, 这意味着,对于包含五个2字节字符的字符串,LENGTH()
returns 10
, whereas CHAR_LENGTH()
returns 5
.LENGTH()
返回10
,而CHAR_LENGTH()
返回5
。
mysql> SELECT LENGTH('text');
-> 4
The Length()
OpenGIS spatial function is named ST_Length()
in MySQL.Length()
OpenGIS 空间函数在MySQL中被命名为ST_Length()
。
Reads the file and returns the file contents as a string. 读取文件并以字符串形式返回文件内容。To use this function, the file must be located on the server host, you must specify the full path name to the file, and you must have the 要使用此功能,文件必须位于服务器主机上,必须指定文件的完整路径名,并且必须具有FILE
privilege. FILE
权限。The file must be readable by the server and its size less than 文件必须可被服务器读取,并且其大小小于max_allowed_packet
bytes. max_allowed_packet
字节数。If the 如果secure_file_priv
system variable is set to a nonempty directory name, the file to be loaded must be located in that directory. secure_file_priv
系统变量设置为非空目录名,则要加载的文件必须位于该目录中。(Prior to MySQL 8.0.17, the file must be readable by all, not just readable by the server.)(在MySQL8.0.17之前,文件必须是所有人都可读的,而不仅仅是服务器可读的。)
If the file does not exist or cannot be read because one of the preceding conditions is not satisfied, the function returns 如果文件不存在或由于不满足上述条件之一而无法读取,则函数将返回NULL
.NULL
。
The character_set_filesystem
system variable controls interpretation of file names that are given as literal strings.character_set_filesystem
系统变量控制以文本字符串形式给出的文件名的解释。
mysql>UPDATE t
SET blob_col=LOAD_FILE('/tmp/picture')
WHERE id=1;
LOCATE(
, substr
,str
)LOCATE(
substr
,str
,pos
)
The first syntax returns the position of the first occurrence of substring 第一种语法返回字符串substr
in string str
. str
中第一个出现的子字符串substr
的位置。The second syntax returns the position of the first occurrence of substring 第二种语法返回字符串substr
in string str
, starting at position pos
. str
中从pos
开始,第一个出现的子字符串substr
的位置。Returns 如果0
if substr
is not in str
. substr
不在str
中,则返回0
。Returns 如果任一参数为NULL
if any argument is NULL
.NULL
,则返回NULL
。
mysql>SELECT LOCATE('bar', 'foobarbar');
-> 4 mysql>SELECT LOCATE('xbar', 'foobar');
-> 0 mysql>SELECT LOCATE('bar', 'foobarbar', 5);
-> 7
This function is multibyte safe, and is case-sensitive only if at least one argument is a binary string.此函数是多字节安全的,并且仅当至少有一个参数是二进制字符串时才区分大小写。
Returns the string 返回字符串str
with all characters changed to lowercase according to the current character set mapping. str
,其中所有字符根据当前字符集映射更改为小写。The default is 默认值为utf8mb4
.utf8mb4
。
mysql> SELECT LOWER('QUADRATICALLY');
-> 'quadratically'
应用于二进制字符串(LOWER()
(and UPPER()
) are ineffective when applied to binary strings (BINARY
, VARBINARY
, BLOB
). BINARY
、VARBINARY
、BLOB
)时,LOWER()
(和UPPER()
)无效。To perform lettercase conversion of a binary string, first convert it to a nonbinary string using a character set appropriate for the data stored in the string:要执行二进制字符串的字母大小写转换,请首先使用适合字符串中存储的数据的字符集将其转换为非二进制字符串:
mysql>SET @str = BINARY 'New York';
mysql>SELECT LOWER(@str), LOWER(CONVERT(@str USING utf8mb4));
+-------------+------------------------------------+ | LOWER(@str) | LOWER(CONVERT(@str USING utf8mb4)) | +-------------+------------------------------------+ | New York | new york | +-------------+------------------------------------+
For collations of Unicode character sets, 对于Unicode字符集的排序规则,LOWER()
and UPPER()
work according to the Unicode Collation Algorithm (UCA) version in the collation name, if there is one, and UCA 4.0.0 if no version is specified. LOWER()
和UPPER()
根据排序规则名称中的Unicode排序规则算法(UCA)版本(如果有)工作,如果没有指定版本,则根据UCA 4.0.0工作。For example, 例如,utf8mb4_0900_ai_ci
and utf8_unicode_520_ci
work according to UCA 9.0.0 and 5.2.0, respectively, whereas utf8_unicode_ci
works according to UCA 4.0.0. utf8mb4_0900_ai_ci
和utf8_unicode_520_ci
分别根据UCA 9.0.0和5.2.0工作,而utf8_unicode_ci
根据UCA 4.0.0工作。See Section 10.10.1, “Unicode Character Sets”.请参阅第10.10.1节,“Unicode字符集”。
This function is multibyte safe.此函数是多字节安全的。
视图中使用的LCASE()
used within views is rewritten as LOWER()
.LCASE()
被重写为LOWER()
。
Returns the string 返回字符串str
, left-padded with the string padstr
to a length of len
characters. str
,用字符串padstr
左填充,长度为len
个字符。If 如果str
is longer than len
, the return value is shortened to len
characters.str
长于len
,则返回值将缩短为len
个字符。
mysql>SELECT LPAD('hi',4,'??');
-> '??hi' mysql>SELECT LPAD('hi',1,'??');
-> 'h'
Returns the string 返回删除前导空格字符的字符串str
with leading space characters removed.str
。
mysql> SELECT LTRIM(' barbar');
-> 'barbar'
This function is multibyte safe.此函数是多字节安全的。
Returns a set value (a string containing substrings separated by 返回一个集值(一个包含由字符,
characters) consisting of the strings that have the corresponding bit in bits
set. ,
分隔的子字符串的字符串),该字符串由bits
集中具有相应位的字符串组成。str1
corresponds to bit 0, str2
to bit 1, and so on. str1
对应于位0,str2
对应于位1,依此类推。NULL
values in str1
, str2
, ...
are not appended to the result.str1
、str2
……不会附加到结果。
mysql>SELECT MAKE_SET(1,'a','b','c');
-> 'a' mysql>SELECT MAKE_SET(1 | 4,'hello','nice','world');
-> 'hello,world' mysql>SELECT MAKE_SET(1 | 4,'hello','nice',NULL,'world');
-> 'hello' mysql>SELECT MAKE_SET(0,'a','b','c');
-> ''
MID(
is a synonym for str
,pos
,len
)SUBSTRING(
.str
,pos
,len
)MID(str,pos,len)
是SUBSTRING(str,pos,len)
的同义词。
Returns a string representation of the octal value of 返回N
, where N
is a longlong (BIGINT
) number. N
的八进制值的字符串表示形式,其中N是longlong(BIGINT
)数。This is equivalent to 这相当于CONV(
. N
,10,8)CONV(
。N
,10,8)Returns 如果N为NULL
if N
is NULL
.NULL
,则返回NULL
。
mysql> SELECT OCT(12);
-> '14'
OCTET_LENGTH()
is a synonym for LENGTH()
.OCTET_LENGTH()
是LENGTH()
的同义词。
If the leftmost character of the string 如果字符串str
is a multibyte character, returns the code for that character, calculated from the numeric values of its constituent bytes using this formula:str
的最左边的字符是多字节字符,则返回该字符的代码,使用以下公式从其组成字节的数值计算得出:
(1st byte code) + (2nd byte code * 256) + (3rd byte code * 256^2) ...
If the leftmost character is not a multibyte character, 如果最左边的字符不是多字节字符,ORD()
returns the same value as the ASCII()
function.ORD()
将返回与ASCII()
函数相同的值。
mysql> SELECT ORD('2');
-> 50
POSITION(
is a synonym for substr
IN str
)LOCATE(
.substr
,str
)
Quotes a string to produce a result that can be used as a properly escaped data value in an SQL statement. 引用字符串以生成一个结果,该结果可用作SQL语句中正确转义的数据值。The string is returned enclosed by single quotation marks and with each instance of backslash (返回的字符串用单引号括起来,每个反斜杠(\
), single quote ('
), ASCII NUL
, and Control+Z preceded by a backslash. \
)、单引号('
)、ASCII NUL
和Control+Z实例前面都有一个反斜杠。If the argument is 如果参数为NULL
, the return value is the word “NULL” without enclosing single quotation marks.NULL
,则返回值为单词“NULL”,不包含单引号。
mysql>SELECT QUOTE('Don\'t!');
-> 'Don\'t!' mysql>SELECT QUOTE(NULL);
-> NULL
For comparison, see the quoting rules for literal strings and within the C API in Section 9.1.1, “String Literals”, and mysql_real_escape_string_quote().要进行比较,请参阅第9.1.1节,“字符串文本”中的文字字符串和C API内的引号规则和mysql_real_escape_String_quote()
。
Returns a string consisting of the string 返回由字符串str
repeated count
times. str
重复count
次数组成的字符串。If 如果count
is less than 1, returns an empty string. count
小于1,则返回空字符串。Returns 如果NULL
if str
or count
are NULL
.str
或count
为NULL
,则返回NULL
。
mysql> SELECT REPEAT('MySQL', 3);
-> 'MySQLMySQLMySQL'
Returns the string 返回字符串str
with all occurrences of the string from_str
replaced by the string to_str
. str
,其中所有出现的字符串from_str
替换为字符串to_str
。REPLACE()
performs a case-sensitive match when searching for from_str
.REPLACE()
在搜索from_str
时执行区分大小写的匹配。
mysql> SELECT REPLACE('www.mysql.com', 'w', 'Ww');
-> 'WwWwWw.mysql.com'
This function is multibyte safe.此函数是多字节安全的。
Returns the string 返回字符顺序颠倒的字符串str
with the order of the characters reversed.str
。
mysql> SELECT REVERSE('abc');
-> 'cba'
This function is multibyte safe.此函数是多字节安全的。
Returns the rightmost 返回字符串len
characters from the string str
, or NULL
if any argument is NULL
.str
中最右边的len
字符,如果任何参数为NULL
,则返回NULL
。
mysql> SELECT RIGHT('foobarbar', 4);
-> 'rbar'
This function is multibyte safe.此函数是多字节安全的。
Returns the string 返回字符串str
, right-padded with the string padstr
to a length of len
characters. str
,用字符串padstr
右填充,长度为len
个字符。If 如果str
is longer than len
, the return value is shortened to len
characters.str
长于len
,则返回值将缩短为len
个字符。
mysql>SELECT RPAD('hi',5,'?');
-> 'hi???' mysql>SELECT RPAD('hi',1,'?');
-> 'h'
This function is multibyte safe.此函数是多字节安全的。
Returns the string 返回删除了尾随空格字符的字符串str
with trailing space characters removed.str
。
mysql> SELECT RTRIM('barbar ');
-> 'barbar'
This function is multibyte safe.此函数是多字节安全的。
Returns a soundex string from 从str
. str
返回soundex字符串。Two strings that sound almost the same should have identical soundex strings. 两个声音几乎相同的弦应该有相同的soundex弦。A standard soundex string is four characters long, but the 标准的soundex字符串有四个字符长,但是SOUNDEX()
function returns an arbitrarily long string. SOUNDEX()
函数返回任意长的字符串。You can use 可以对结果使用SUBSTRING()
on the result to get a standard soundex string. SUBSTRING()
来获取标准的soundex字符串。All nonalphabetic characters in str
are ignored. str
中的所有非字母字符都被忽略。All international alphabetic characters outside the A-Z range are treated as vowels.A-Z范围以外的所有国际字母字符都被视为元音。
When using 使用SOUNDEX()
, you should be aware of the following limitations:SOUNDEX()
时,应注意以下限制:
This function, as currently implemented, is intended to work well with strings that are in the English language only. 目前实现的这个函数可以很好地处理只使用英语的字符串。Strings in other languages may not produce reliable results.其他语言的字符串可能不会产生可靠的结果。
This function is not guaranteed to provide consistent results with strings that use multibyte character sets, including 此函数不能保证为使用多字节字符集(包括utf-8)的字符串提供一致的结果。utf-8
. See Bug #22638 for more information.更多信息请参见Bug#22638。
mysql>SELECT SOUNDEX('Hello');
-> 'H400' mysql>SELECT SOUNDEX('Quadratically');
-> 'Q36324'
This function implements the original Soundex algorithm, not the more popular enhanced version (also described by D. Knuth). 这个函数实现了原始的Soundex算法,而不是更流行的增强版(也由D。克努特)。The difference is that original version discards vowels first and duplicates second, whereas the enhanced version discards duplicates first and vowels second.不同之处在于,原始版本首先丢弃元音,然后重复元音,而增强版本则首先丢弃重复元音,然后重复元音。
This is the same as 这相当于SOUNDEX(
.expr1
) = SOUNDEX(expr2
)SOUNDEX(
。expr1
) = SOUNDEX(expr2
)
Returns a string consisting of 返回由N
space characters.N
个空格字符组成的字符串。
mysql> SELECT SPACE(6);
-> ' '
SUBSTR(
, str
,pos
)SUBSTR(
, str
FROM pos
)SUBSTR(
, str
,pos
,len
)SUBSTR(
str
FROM pos
FOR len
)
SUBSTR()
is a synonym for SUBSTRING()
.SUBSTR()
是SUBSTRING()
的同义词。
SUBSTRING(
, str
,pos
)SUBSTRING(
, str
FROM pos
)SUBSTRING(
, str
,pos
,len
)SUBSTRING(
str
FROM pos
FOR len
)
The forms without a 不带len
argument return a substring from string str
starting at position pos
. len
参数的形式从位置pos
开始返回字符串str
的子字符串。The forms with a 带有len
argument return a substring len
characters long from string str
, starting at position pos
. len
参数的形式返回字符串str
中长度为len
个字符的子字符串,从位置pos
开始。The forms that use 使用FROM
are standard SQL syntax. FROM
的形式是标准的SQL语法。It is also possible to use a negative value for pos
. pos
也可以使用负值。In this case, the beginning of the substring is 在本例中,子字符串的开头是从字符串末尾开始数pos
characters from the end of the string, rather than the beginning. pos
字符,而不是从开头开始数。A negative value may be used for 此函数的任何形式的pos
in any of the forms of this function. pos
都可以使用负值。A value of 0 for pos
returns an empty string.pos
的值为0将返回空字符串。
For all forms of 对于SUBSTRING()
, the position of the first character in the string from which the substring is to be extracted is reckoned as 1
.SUBSTRING()
的所有形式,要从中提取子字符串的字符串中第一个字符的位置都被认为是1
。
mysql>SELECT SUBSTRING('Quadratically',5);
-> 'ratically' mysql>SELECT SUBSTRING('foobarbar' FROM 4);
-> 'barbar' mysql>SELECT SUBSTRING('Quadratically',5,6);
-> 'ratica' mysql>SELECT SUBSTRING('Sakila', -3);
-> 'ila' mysql>SELECT SUBSTRING('Sakila', -5, 3);
-> 'aki' mysql>SELECT SUBSTRING('Sakila' FROM -4 FOR 2);
-> 'ki'
This function is multibyte safe.此函数是多字节安全的。
If 如果len
is less than 1, the result is the empty string.len
小于1,则结果为空字符串。
SUBSTRING_INDEX(
str
,delim
,count
)
Returns the substring from string 在分隔符str
before count
occurrences of the delimiter delim
. delim
出现计数之前,返回字符串str
中的子字符串。If 如果count
is positive, everything to the left of the final delimiter (counting from the left) is returned. count
为正,则返回最后定界符左侧的所有内容(从左侧开始计数)。If 如果count
is negative, everything to the right of the final delimiter (counting from the right) is returned. count
为负数,则返回最后定界符右侧的所有内容(从右侧开始计数)。SUBSTRING_INDEX()
performs a case-sensitive match when searching for delim
.SUBSTRING_INDEX()
在搜索delim
时执行区分大小写的匹配。
mysql>SELECT SUBSTRING_INDEX('www.mysql.com', '.', 2);
-> 'www.mysql' mysql>SELECT SUBSTRING_INDEX('www.mysql.com', '.', -2);
-> 'mysql.com'
This function is multibyte safe.此函数是多字节安全的。
Converts the string argument to base-64 encoded form and returns the result as a character string with the connection character set and collation. 将字符串参数转换为base-64编码形式,并将结果作为包含连接字符集和排序规则的字符串返回。If the argument is not a string, it is converted to a string before conversion takes place. 如果参数不是字符串,则在转换之前将其转换为字符串。The result is 如果参数为NULL
if the argument is NULL
. NULL
,则结果为NULL
。Base-64 encoded strings can be decoded using the Base-64编码的字符串可以使用FROM_BASE64()
function.FROM_BASE64()
函数进行解码。
mysql> SELECT TO_BASE64('abc'), FROM_BASE64(TO_BASE64('abc'));
-> 'JWJj', 'abc'
Different base-64 encoding schemes exist. 存在不同的base-64编码方案。These are the encoding and decoding rules used by 以下是TO_BASE64()
and FROM_BASE64()
:TO_BASE64()
和FROM_BASE64()
使用的编码和解码规则:
The encoding for alphabet value 62 is 字母表值62的编码为'+'
.'+'
。
The encoding for alphabet value 63 is 字母值63的编码为'/'
.'/'
。
Encoded output consists of groups of 4 printable characters. 编码输出由4个可打印字符组成。Each 3 bytes of the input data are encoded using 4 characters. 输入数据的每3个字节用4个字符编码。If the last group is incomplete, it is padded with 如果最后一组不完整,则用'='
characters to a length of 4.'='
字符填充,长度为4。
A newline is added after each 76 characters of encoded output to divide long output into multiple lines.在编码输出的每76个字符后添加一个换行符,将长输出分成多行。
Decoding recognizes and ignores newline, carriage return, tab, and space.解码识别并忽略换行符、回车符、制表符和空格。
TRIM([{BOTH | LEADING | TRAILING} [
, remstr
] FROM] str
)TRIM([
remstr
FROM] str
)
Returns the string 返回删除了所有str
with all remstr
prefixes or suffixes removed. remstr
前缀或后缀的字符串str
。If none of the specifiers 如果没有同时给定任何说明符BOTH
, LEADING
, or TRAILING
is given, BOTH
is assumed. BOTH
、LEADING
或TRAILING
,则假定两者都是。remstr
is optional and, if not specified, spaces are removed.remstr
是可选的,如果未指定,则删除空格。
mysql>SELECT TRIM(' bar ');
-> 'bar' mysql>SELECT TRIM(LEADING 'x' FROM 'xxxbarxxx');
-> 'barxxx' mysql>SELECT TRIM(BOTH 'x' FROM 'xxxbarxxx');
-> 'bar' mysql>SELECT TRIM(TRAILING 'xyz' FROM 'barxxyz');
-> 'barx'
This function is multibyte safe.此函数是多字节安全的。
UCASE()
is a synonym for UPPER()
.UCASE()
是UPPER()
的同义词。
视图中使用的UCASE()
used within views is rewritten as UPPER()
.UCASE()
被重写为UPPER()
。
For a string argument 对于字符串参数str
, UNHEX(
interprets each pair of characters in the argument as a hexadecimal number and converts it to the byte represented by the number. str
)str
,UNHEX(str)
将参数中的每对字符解释为十六进制数,并将其转换为该数字表示的字节。The return value is a binary string.返回值是二进制字符串。
mysql>SELECT UNHEX('4D7953514C');
-> 'MySQL' mysql>SELECT X'4D7953514C';
-> 'MySQL' mysql>SELECT UNHEX(HEX('string'));
-> 'string' mysql>SELECT HEX(UNHEX('1267'));
-> '1267'
The characters in the argument string must be legal hexadecimal digits: 参数字符串中的字符必须是合法的十六进制数字:'0'
.. '9'
, 'A'
.. 'F'
, 'a'
.. 'f'
. '0'
.. '9'
、'A'
.. 'F'
、'a'
.. 'f'
。If the argument contains any nonhexadecimal digits, the result is 如果参数包含任何非十六进制数字,则结果为NULL
:NULL
:
mysql> SELECT UNHEX('GG');
+-------------+
| UNHEX('GG') |
+-------------+
| NULL |
+-------------+
A 如果NULL
result can occur if the argument to UNHEX()
is a BINARY
column, because values are padded with 0x00
bytes when stored but those bytes are not stripped on retrieval. UNHEX()
的参数是BINARY
列,则会出现NULL
结果,因为存储时会用0x00
字节填充值,但在检索时不会剥离这些字节。For example, 例如,'41'
is stored into a CHAR(3)
column as '41 '
and retrieved as '41'
(with the trailing pad space stripped), so UNHEX()
for the column value returns X'41'
. '41'
作为'41 '
存储在CHAR(3)
列中并检索为'41'
(去掉尾随填充空格),因此列值的UNHEX()
返回X'41'
。By contrast, 相比之下,'41'
is stored into a BINARY(3)
column as '41\0'
and retrieved as '41\0'
(with the trailing pad 0x00
byte not stripped). '41'
作为'41\0'
存储在BINARY(3)
列中,并作为'41\0'
检索(尾部填充0x00
字节未剥离)。'\0'
is not a legal hexadecimal digit, so UNHEX()
for the column value returns NULL
.'\0'
不是合法的十六进制数字,因此列值的UNHEX()
返回NULL
。
For a numeric argument 对于数值参数N
, the inverse of HEX(
is not performed by N
)UNHEX()
. N
,UNHEX()
不执行HEX(
的逆运算。N
)Use 请改用CONV(HEX(
instead. N
),16,10)CONV(HEX(
。N
),16,10)See the description of 请参见HEX()
.HEX()
的说明。
If 如果从mysql客户机中调用UNHEX()
is invoked from within the mysql client, binary strings display using hexadecimal notation, depending on the value of the --binary-as-hex
. UNHEX()
,则二进制字符串将使用十六进制表示法显示,具体取决于--binary-as-hex
的值。For more information about that option, see Section 4.5.1, “mysql — The MySQL Command-Line Client”.有关该选项的更多信息,请参阅第4.5.1节,“mysql命令行客户端”。
Returns the string 返回字符串str
with all characters changed to uppercase according to the current character set mapping. str
,其中所有字符根据当前字符集映射更改为大写。The default is 默认值为utf8mb4
.utf8mb4
。
mysql> SELECT UPPER('Hej');
-> 'HEJ'
See the description of 有关同样适用于LOWER()
for information that also applies to UPPER()
. UPPER()
的信息,请参见LOWER()
的说明。This included information about how to perform lettercase conversion of binary strings (这包括有关如何对这些函数无效的二进制字符串(BINARY
, VARBINARY
, BLOB
) for which these functions are ineffective, and information about case folding for Unicode character sets.BINARY
、VARBINARY
、BLOB
)执行字母大小写转换的信息,以及有关Unicode字符集的大小写折叠的信息。
This function is multibyte safe.此函数是多字节安全的。
视图中使用的UCASE()
used within views is rewritten as UPPER()
.UCASE()
被重写为UPPER()
。
WEIGHT_STRING(
str
[AS {CHAR|BINARY}(N
)] [flags
])
This function returns the weight string for the input string. 此函数返回输入字符串的权重字符串。The return value is a binary string that represents the comparison and sorting value of the string. 返回值是一个二进制字符串,表示字符串的比较和排序值。It has these properties:它具有以下特性:
If WEIGHT_STRING(
= str1
)WEIGHT_STRING(
, then str2
)
(str1
= str2
str1
and str2
are considered equal)
If 如果WEIGHT_STRING(
< str1
)WEIGHT_STRING(
, then str2
)
(str1
< str2
str1
sorts before str2
)WEIGHT_STRING(
,则str1
) < WEIGHT_STRING(str2
)
(str1
< str2
str1
排在str2
之前)
WEIGHT_STRING()
is a debugging function intended for internal use. WEIGHT_STRING()
是供内部使用的调试函数。Its behavior can change without notice between MySQL versions. 在MySQL版本之间,它的行为可以在没有通知的情况下更改。It can be used for testing and debugging of collations, especially if you are adding a new collation. 它可以用于测试和调试排序规则,特别是在添加新排序规则时。See Section 10.14, “Adding a Collation to a Character Set”.请参阅第10.14节,“向字符集添加排序规则”。
This list briefly summarizes the arguments. 这个列表简要地总结了这些论点。More details are given in the discussion following the list.更多细节在下面的讨论中给出。
str
: The input string expression.:输入字符串表达式。
AS
clause: Optional; cast the input string to a given type and length.子句:可选;将输入字符串强制转换为给定的类型和长度。
flags
: Optional; unused.:可选;未使用。
The input string, 输入字符串str
, is a string expression. str
是一个字符串表达式。If the input is a nonbinary (character) string such as a 如果输入是非二进制(字符)字符串(如CHAR
, VARCHAR
, or TEXT
value, the return value contains the collation weights for the string. CHAR
、VARCHAR
或TEXT
值),则返回值包含该字符串的排序规则权重。If the input is a binary (byte) string such as a 如果输入是二进制(字节)字符串,如BINARY
, VARBINARY
, or BLOB
value, the return value is the same as the input (the weight for each byte in a binary string is the byte value). BINARY
、VARBINARY
或BLOB
值,则返回值与输入值相同(二进制字符串中每个字节的权重为byte值)。If the input is 如果输入为NULL
, WEIGHT_STRING()
returns NULL
.NULL
,WEIGHT_STRING()
将返回NULL
。
Examples:例如:
mysql>SET @s = _utf8mb4 'AB' COLLATE utf8mb4_0900_ai_ci;
mysql>SELECT @s, HEX(@s), HEX(WEIGHT_STRING(@s));
+------+---------+------------------------+ | @s | HEX(@s) | HEX(WEIGHT_STRING(@s)) | +------+---------+------------------------+ | AB | 4142 | 1C471C60 | +------+---------+------------------------+
mysql>SET @s = _utf8mb4 'ab' COLLATE utf8mb4_0900_ai_ci;
mysql>SELECT @s, HEX(@s), HEX(WEIGHT_STRING(@s));
+------+---------+------------------------+ | @s | HEX(@s) | HEX(WEIGHT_STRING(@s)) | +------+---------+------------------------+ | ab | 6162 | 1C471C60 | +------+---------+------------------------+
mysql>SET @s = CAST('AB' AS BINARY);
mysql>SELECT @s, HEX(@s), HEX(WEIGHT_STRING(@s));
+------+---------+------------------------+ | @s | HEX(@s) | HEX(WEIGHT_STRING(@s)) | +------+---------+------------------------+ | AB | 4142 | 4142 | +------+---------+------------------------+
mysql>SET @s = CAST('ab' AS BINARY);
mysql>SELECT @s, HEX(@s), HEX(WEIGHT_STRING(@s));
+------+---------+------------------------+ | @s | HEX(@s) | HEX(WEIGHT_STRING(@s)) | +------+---------+------------------------+ | ab | 6162 | 6162 | +------+---------+------------------------+
The preceding examples use 前面的示例使用HEX()
to display the WEIGHT_STRING()
result. HEX()
来显示WEIGHT_STRING()
结果。Because the result is a binary value, 因为结果是一个二进制值,所以HEX()
can be especially useful when the result contains nonprinting values, to display it in printable form:HEX()
在结果包含非打印值时特别有用,以可打印形式显示:
mysql>SET @s = CONVERT(X'C39F' USING utf8) COLLATE utf8_czech_ci;
mysql>SELECT HEX(WEIGHT_STRING(@s));
+------------------------+ | HEX(WEIGHT_STRING(@s)) | +------------------------+ | 0FEA0FEA | +------------------------+
For non-对于非NULL
return values, the data type of the value is VARBINARY
if its length is within the maximum length for VARBINARY
, otherwise the data type is BLOB
.NULL
返回值,如果值的长度在VARBINARY
的最大长度内,则该值的数据类型为VARBINARY
,否则该数据类型为BLOB
。
The AS
clause may be given to cast the input string to a nonbinary or binary string and to force it to a given length:AS
子句可用于将输入字符串强制转换为非二进制或二进制字符串,并将其强制转换为给定长度:
AS CHAR(
casts the string to a nonbinary string and pads it on the right with spaces to a length of N
)N
characters. AS CHAR(
将字符串强制转换为非二进制字符串,并在右侧填充长度为N
)N
个字符的空格。N
must be at least 1. N
必须至少为1。If 如果N
is less than the length of the input string, the string is truncated to N
characters. No warning occurs for truncation.N
小于输入字符串的长度,则字符串将被截断为N
个字符。不会出现截断警告。
虽然AS BINARY(
is similar but casts the string to a binary string, N
)N
is measured in bytes (not characters), and padding uses 0x00
bytes (not spaces).AS BINARY(
类似,但将字符串强制转换为二进制字符串,因此N
)N
的单位是字节(而不是字符),而padding使用0x00
字节(而不是空格)。
mysql>SET NAMES 'latin1';
mysql>SELECT HEX(WEIGHT_STRING('ab' AS CHAR(4)));
+-------------------------------------+ | HEX(WEIGHT_STRING('ab' AS CHAR(4))) | +-------------------------------------+ | 41422020 | +-------------------------------------+ mysql>SET NAMES 'utf8';
mysql>SELECT HEX(WEIGHT_STRING('ab' AS CHAR(4)));
+-------------------------------------+ | HEX(WEIGHT_STRING('ab' AS CHAR(4))) | +-------------------------------------+ | 0041004200200020 | +-------------------------------------+
mysql> SELECT HEX(WEIGHT_STRING('ab' AS BINARY(4)));
+---------------------------------------+
| HEX(WEIGHT_STRING('ab' AS BINARY(4))) |
+---------------------------------------+
| 61620000 |
+---------------------------------------+
The flags
clause currently is unused.flags
子句当前未使用。
If 如果从mysql客户机中调用WEIGHT_STRING()
is invoked from within the mysql client, binary strings display using hexadecimal notation, depending on the value of the --binary-as-hex
. WEIGHT_STRING()
,则二进制字符串将使用十六进制表示法显示,具体取决于--binary-as-hex
的值。For more information about that option, see Section 4.5.1, “mysql — The MySQL Command-Line Client”.有关该选项的更多信息,请参阅第4.5.1节,“mysql命令行客户端”。