mysql字符串函数

文章目录

    • ASCII(str)
    • BIN(N)
    • BIT_LENGTH(str)
    • CHAR(N,... [USING charset_name])16进制
    • CHAR_LENGTH(str)\CHARACTER_LENGTH(str)
    • CONCAT(str1,str2,...)
    • CONCAT_WS(separator,str1,str2,...)
    • ELT(N,str1,str2,str3,...)
    • FIELD(str,str1,str2,str3,...)
    • FIND_IN_SET(str,strlist)
    • FORMAT(X,D[,locale])
    • FROM_BASE64(str)
    • HEX(str), HEX(N)
    • INSERT(str,pos,len,newstr)
    • INSTR(str,substr)
    • LEFT(str,len)
    • LENGTH(str)
    • LOAD_FILE(file_name)
    • LOCATE(substr,str), LOCATE(substr,str,pos)
    • LOWER(str)
    • LPAD(str,len,padstr)
    • LTRIM(str)
    • MAKE_SET(bits,str1,str2,...)
    • OCT(N)
    • QUOTE(str)
    • REPEAT(str,count)
    • REPLACE(str,from_str,to_str)
    • RPAD(str,len,padstr)
    • RTRIM(str)
    • SPACE(N)
    • SUBSTRING(str,pos), SUBSTRING(str FROM pos), SUBSTRING(str,pos,len), SUBSTRING(str FROM pos FOR len)
    • SUBSTRING_INDEX(str,delim,count)
    • TO_BASE64(str)
    • TRIM([{BOTH | LEADING | TRAILING} [remstr] FROM] str), TRIM([remstr FROM] str)
    • UNHEX(str)
    • UPPER(str)

https://dev.mysql.com/doc/refman/8.0/en/string-functions.html

如果字符串值函数的长度大于max_allowed_packet系统变量的值,则字符串值函数返回NULL。
对于操作字符串位置的函数,第一个位置的编号为1。 对于接受长度参数的函数,非整数参数四舍五入到最近的整数。

ASCII(str)

Returns the numeric value of the leftmost character of the string str. Returns 0 if str is the empty string. Returns NULL if str is NULL. ASCII() works for 8-bit characters.
返回字符串str最左边一个字符的ASCII值的十进制的表现形式。如果str是空字符串,则返回0。如果str为空,则返回NULL。ASCII()适用于8位字符。ASCII参考 10进制
https://simple.wikipedia.org/wiki/ASCII

SELECT ASCII('2'),ASCII(2),ASCII('d'),ASCII('ds');

在这里插入图片描述

BIN(N)

Returns a string representation of the binary value of N, where N is a longlong (BIGINT) number. This is equivalent to CONV(N,10,2). Returns NULL if N is NULL.
返回数字N的二进制的字符串形式,N为number

SELECT BIN(12),BIN(-12)  -- 1100  (8421码)=8+4=12  返回一个数的二进制  正整数的原码、反码和补码是一样的

在这里插入图片描述

BIT_LENGTH(str)

Returns the length of the string str in bits.
返回这个字符串的位数

SELECT BIT_LENGTH('text'); -- 32 一个字符占8位 1个字节
SELECT BIT_LENGTH('tex'); -- 24  
SELECT BIT_LENGTH(tex)   -- Unknown column 'tex' in 'field list'
SELECT BIT_LENGTH('中国'); -- 48 一个汉子占24位  3个字节
	  
SELECT BIT_LENGTH('好');  -- 24
	  
SELECT BIT_LENGTH(1);  -- 8
SELECT BIT_LENGTH(11);  -- 16

CHAR(N,… [USING charset_name])16进制

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解释为一个整数,并返回ASCII十进制等于这些整数的的 ASCII字符 空值被跳过。

SELECT CHAR(77,121,83,81,'76'); -- MySQL
SELECT CHAR(77,77.3,'77.3'); -- MMM

CHAR() arguments larger than 255 are converted into multiple result bytes. For example, CHAR(256) is equivalent to CHAR(1,0), and CHAR(256256) is equivalent toCHAR(1,0,0)
大于255的CHAR()参数被转换成多个结果字节。例如,CHAR(256)等价于CHAR(1,0), CHAR(256
256)等价于CHAR(1,0,0)
mysql字符串函数_第1张图片
By default, CHAR() returns a binary string. To produce a string in a given character set, use the optional USING clause:
默认情况下,CHAR()返回一个二进制字符串。若要在给定字符集中生成字符串,请使用可选的USING子句:
mysql字符串函数_第2张图片

如果使用的是给定的,并且结果字符串对于给定的字符集是非法的,则会发出警告。此外,如果启用了严格的SQL模式,CHAR()的结果将变为NULL。
SELECT CHAR(77); – M
SELECT CHARSET(CHAR(77)) – binary

SELECT CHAR(65)    -- A
SELECT CHAR('65')  -- A
SELECT CHAR(X'65'); -- e  x(不区分大小写)开头的为16进制
SELECT CHAR(x65);  -- 错误代码: 1054 Unknown column 'x65' in 'field list'

CHAR_LENGTH(str)\CHARACTER_LENGTH(str)

Returns the length of the string str, measured in characters. A multibyte character counts as a single character. This means that for a string containing five 2-byte characters, LENGTH() returns 10, whereas CHAR_LENGTH() returns 5.
返回字符串str的长度,以字符为度量单位。
多字节字符被视为单个字符。
这意味着对于包含5个2字节字符的字符串,LENGTH()返回10,而CHAR_LENGTH()返回5。

SELECT CHAR_LENGTH('你好'),CHAR_LENGTH('hello')

在这里插入图片描述
CHARACTER_LENGTH() is a synonym for CHAR_LENGTH().
CHARACTER_LENGTH()是CHAR_LENGTH()的同义词。

CONCAT(str1,str2,…)

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字符串函数_第3张图片
For quoted strings, concatenation can be performed by placing the strings next to each other:
对于带引号的字符串,可以通过将字符串放在一起来执行连接:

在这里插入图片描述

SELECT '你好' '世界','你好''世界'; -- 字符串之间用空格分开

在这里插入图片描述

CONCAT_WS(separator,str1,str2,…)

CONCAT_WS() stands for Concatenate With Separator and is a special form of 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.

CONCAT_WS()代表Concatenate With Separator,是CONCAT()的一种特殊形式。第一个参数是其余参数的分隔符。分隔符被添加到要连接的字符串之间。分隔符可以是字符串,其他参数也可以是字符串。如果分隔符为空,则结果为空。在这里插入图片描述

CONCAT_WS() does not skip empty strings. However, it does skip any NULL values after the separator argument.

ELT(N,str1,str2,str3,…)

ELT() returns the Nth element of the list of strings: str1 if N = 1, str2 if N = 2, and so on. Returns NULL if N is less than 1 or greater than the number of arguments.ELT() is the complement of FIELD().
ELT()返回字符串列表的第N个元素:str1(如果N = 1), str2(如果N = 2),以此类推。如果N小于1或大于参数的数量,则返回NULL。ELT()是FIELD()的补码。

在这里插入图片描述

FIELD(str,str1,str2,str3,…)

返回str1、str2、str3、…列表。如果没有找到str,则返回0。

如果FIELD()的所有参数都是字符串,那么所有参数都将作为字符串进行比较。如果所有参数都是数字,则将它们作为数字进行比较。否则,将参数比较为double。

如果str为NULL,返回值为0,因为NULL不能与任何值进行相等比较。FIELD()是ELT()的补码。

在这里插入图片描述

FIND_IN_SET(str,strlist)

Returns a value in the range of 1 to N if the string str is in the string list strlist consisting of N substrings. 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. Returns 0 if str is not in strlist or if strlist is the empty string. Returns NULL if either argument is NULL. This function does not work properly if the first argument contains a comma (,) character.
如果字符串str位于由N个子字符串组成的字符串列表中,则返回一个范围为1到N的值。
字符串列表是由字符分隔的子字符串组成的字符串。
如果第一个参数是常量字符串,第二个参数是SET类型的列,则FIND_IN_SET()函数被优化为使用位算法。如果str不在strlist中,或者strlist是空字符串,则返回0。如果其中一个参数为空,则返回NULL。如果第一个参数包含逗号(,)字符,则此函数不能正常工作。
在这里插入图片描述

FORMAT(X,D[,locale])

Formats the number X to a format like ‘#,###,###.##’, rounded to D decimal places, and returns the result as a string. If D is 0, the result has no decimal point or fractional part.

将数字X格式化为’#,###,###。,四舍五入到D位小数,并以字符串形式返回结果。如果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.15, “MySQL Server Locale Support”). If no locale is specified, the default is ‘en_US’.
可选的第三个参数允许指定区域设置,用于结果号的小数点、千位分隔符和分隔符之间的分组。允许的区域设置值与lc_time_names系统变量的合法值相同(参见10.15节,“MySQL服务器区域设置支持”)。如果没有指定区域设置,默认值是’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'

FROM_BASE64(str)

Takes a string encoded with the base-64 encoded rules used by TO_BASE64() and returns the decoded result as a binary string. The result is NULL if the argument isNULL or not a valid base-64 string. See the description of TO_BASE64() for details about the encoding and decoding rules.
接受一个用TO_BASE64()使用的base-64编码规则编码的字符串,并将解码后的结果作为二进制字符串返回。如果参数为NULL或不是有效的base-64字符串,则结果为NULL。有关编码和解码规则的详细信息,请参阅TO_BASE64()的描述。

mysql> SELECT TO_BASE64('abc'), FROM_BASE64(TO_BASE64('abc'));
        -> 'JWJj', 'abc'

HEX(str), HEX(N)

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. (Multibyte characters therefore become more than two digits.) The inverse of this operation is performed by the UNHEX() function.
对于字符串参数str, HEX()返回str的十六进制字符串表示形式,其中str中的每个字符的每个字节都转换为两个十六进制数字。(因此,多字节字符会变成两个以上的数字。)此操作的逆函数由UNHEX()函数执行。
For a numeric argument N, HEX() returns a hexadecimal string representation of the value of N treated as a longlong (BIGINT) number. This is equivalent toCONV(N,10,16). The inverse of this operation is performed by CONV(HEX(N),16,10).
对于数值参数N, HEX()返回一个十六进制字符串,表示作为longlong (BIGINT)数字处理的N的值。它等价于CONV(N,10,16)此操作的逆操作由CONV(十六进制(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

SELECT HEX(16) -- 10
SELECT HEX(17) -- 11

SELECT HEX('a') -- 61
SELECT HEX(97)  -- 61

SELECT HEX('ab') -- 6161

SELECT UNHEX(HEX('ab')) 

SELECT UNHEX(X'61') 

SELECT X'616263', HEX('abc'), UNHEX(HEX('abc'));
 
SELECT HEX(255), CONV(HEX(255),16,10);

SELECT 0XA

MySql_十六进制值https://www.cnblogs.com/yufei121/p/6103336.html
十六进制值
MySQL支持十六进制值。在数字上下文中,十六进制数如同整数(64位精度)。在字符串上下文,如同二进制字符串,每对十六进制数字被转换为一个字符:

mysql> SELECT x'4D7953514C';
        -> 'MySQL'
mysql> SELECT 0xa+0;
        -> 10
mysql> SELECT 0x5061756c;
        -> 'Paul'

十六进制值的默认类型是字符串。如果想要确保该值作为数字处理,可以使用CAST(…AS UNSIGNED):

mysql> SELECT 0x41,CAST(0x41 AS UNSIGNED);
        -> 'A',65

0x语法基于ODBC。十六进制字符串通常用于ODBC以便为BLOB列提供值。
x’hexstring’语法基于标准SQL。
可以用HEX()函数将一个字符串或数字转换为十六进制格式的字符串:

mysql> SELECT HEX('cat');
        -> '636174'
mysql> SELECT 0x636174;
        -> 'cat'

INSERT(str,pos,len,newstr)

Returns the string str, with the substring beginning at position pos and len characters long replaced by the string newstr. Returns the original string if pos is not within the length of the string. Replaces the rest of the string from position pos if len is not within the length of the rest of the string. Returns NULL if any argument is 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'

返回字符串str,子字符串以pos位置开始,len字符由字符串newstr替换。如果pos不在字符串长度内,则返回原始字符串。如果len不在字符串其余部分的长度范围内,则从位置pos替换字符串的其余部分。如果任何参数为空,则返回NULL。

INSTR(str,substr)

Returns the position of the first occurrence of substring substr in string str. This is the same as the two-argument form of LOCATE(), except that the order of the arguments is reversed.

返回字符串str中子字符串substr第一次出现的位置。这与LOCATE()的双参数形式相同,只是参数的顺序颠倒了。 从1开始

mysql> SELECT INSTR('foobarbar', 'bar');
        -> 4
mysql> SELECT INSTR('xbar', 'foobar');
        -> 0

LEFT(str,len)

Returns the leftmost len characters from the string str, or NULL if any argument is NULL.
返回字符串str中最左边的len字符,如果参数为空,则返回NULL。 从1开始

mysql> SELECT LEFT('foobarbar', 5);
        -> 'fooba'

LENGTH(str)

Returns the length of the string str, measured in bytes. A multibyte character counts as multiple bytes. This means that for a string containing five 2-byte characters,LENGTH() returns 10, whereas CHAR_LENGTH() returns 5.

mysql> SELECT LENGTH('text');
        -> 4

LOAD_FILE(file_name)

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. The file must be readable by all and its size less than max_allowed_packet bytes. 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.
If the file does not exist or cannot be read because one of the preceding conditions is not satisfied, the function returns NULL.
The character_set_filesystem system variable controls interpretation of file names that are given as literal strings.

mysql> UPDATE t
            SET blob_col=LOAD_FILE('/tmp/picture')
            WHERE id=1;

https://segmentfault.com/a/1190000009333563
https://jingyan.baidu.com/article/e8cdb32bf15b3437052bad17.html
https://dev.mysql.com/doc/refman/8.0/en/server-options.html#option_mysqld_secure-file-priv

读取文件并以字符串的形式返回文件内容。要使用此函数,文件必须位于服务器主机上,必须指定文件的完整路径名,并且必须具有文件特权。文件必须是所有人都可读的,并且它的大小小于max_allowed_packet字节。如果secure_file_priv系统变量被设置为非空目录名,则要加载的文件必须位于该目录中。

如果文件不存在或由于不满足前面的条件之一而无法读取,则函数返回NULL。

character_set_filesystem系统变量控制以文字字符串形式给出的文件名的解释。

LOCATE(substr,str), LOCATE(substr,str,pos)

The first syntax returns the position of the first occurrence of substring substr in string str. The second syntax returns the position of the first occurrence of substring substr in string str, starting at position pos. Returns 0 if substr is not in str. Returns NULL if any argument is NULL.

mysql> SELECT LOCATE('bar', 'foobarbar');
        -> 4
mysql> SELECT LOCATE('xbar', 'foobar');
        -> 0
mysql> SELECT LOCATE('bar', 'foobarbar', 5);
        -> 7

第一个语法返回第一次出现的位置的子串的子串字符串str。
第二个语法返回第一次出现的位置字符串str子串的子串的开始位置pos。
返回0,如果字符串的子串不在str。返回NULL如果任何参数是NULL。

LOWER(str)

Returns the string str with all characters changed to lowercase according to the current character set mapping. The default is utf8mb4.

mysql> SELECT LOWER('QUADRATICALLY');
        -> 'quadratically'
LOWER() (and UPPER()) are ineffective when applied to binary strings (BINARY, VARBINARY, BLOB). To perform lettercase conversion, convert the string to a nonbinary 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, 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. For example, utf8mb4_0900_ai_ci and utf8_unicode_520_ci work according to UCA 9.0.0 and 5.2.0, respectively, whereasutf8_unicode_ci works according to UCA 4.0.0. See Section 10.10.1, “Unicode Character Sets”.
当将LOWER()(和UPPER())应用于二进制字符串(binary、VARBINARY、BLOB)时无效。要执行字母大小写转换,请将字符串转换为非二进制字符串:

LPAD(str,len,padstr)

Returns the string str, left-padded with the string padstr to a length of len characters. If str is longer than len, the return value is shortened to len characters.

mysql> SELECT LPAD('hi',4,'??');
        -> '??hi'
mysql> SELECT LPAD('hi',1,'??');
        -> 'h'

返回字符串str,用字符串padstr左填充为len字符长度。如果str比len长,则返回值缩短为len字符。

LTRIM(str)

Returns the string str with leading space characters removed.

mysql> SELECT LTRIM('  barbar');
        -> 'barbar'

MAKE_SET(bits,str1,str2,…)

Returns a set value (a string containing substrings separated by , characters) consisting of the strings that have the corresponding bit in bits set. str1 corresponds to bit 0, str2 to bit 1, and so on. NULL values in str1, str2, … are not appended to the result.

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');
        -> ''

https://blog.csdn.net/qq_41725312/article/details/83039525

OCT(N)

Returns a string representation of the octal value of N, where N is a longlong (BIGINT) number. This is equivalent to CONV(N,10,8). Returns NULL if N is NULL.

mysql> SELECT OCT(12);
        -> '14'
返回一个字符串表示的八进制值N,其中N是一个long - long (BIGINT)数字。它等于CONV(N,10,8)如果N为空,返回NULL。
SELECT OCT('8') – 10

QUOTE(str)

Quotes a string to produce a result that can be used as a properly escaped data value in an SQL statement. 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. If the argument is NULL, the return value is the word “NULL”without enclosing single quotation marks.

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 Section 28.7.7.56, “mysql_real_escape_string_quote()”.
引用一个字符串来生成一个结果,该结果可以用作SQL语句中正确转义的数据值。返回的字符串用单引号括起来,每个反斜杠()、单引号(’)、ASCII NUL和Control+Z的实例前都有一个反斜杠。如果参数为NULL,返回值为单词“NULL”,不包含单引号。

QUOTE()经常用在防止sql注入

SELECT org_name FROM auth_org WHERE org_name LIKE QUOTE('%省区')   
SELECT org_name FROM auth_org WHERE org_name LIKE '%省区'   

mysql字符串函数_第4张图片

REPEAT(str,count)

Returns a string consisting of the string str repeated count times. If count is less than 1, returns an empty string. Returns NULL if str or count are NULL.

mysql> SELECT REPEAT('MySQL', 3);
        -> 'MySQLMySQLMySQL'

返回由字符串str重复计数次数组成的字符串。如果count小于1,返回一个空字符串。如果str或count为空,则返回NULL。

REPLACE(str,from_str,to_str)

Returns the string str with all occurrences of the string from_str replaced by the string to_str. REPLACE() performs a case-sensitive match when searching forfrom_str.

mysql> SELECT REPLACE('www.mysql.com', 'w', 'Ww');
        -> 'WwWwWw.mysql.com'

返回字符串str,将出现的所有from_str字符串替换为字符串to_str。REPLACE()在搜索from_str时执行区分大小写的匹配。

REVERSE(str)
Returns the string str with the order of the characters reversed.
mysql> SELECT REVERSE(‘abc’);
-> ‘cba’
RIGHT(str,len)
Returns the rightmost len characters from the string str, or NULL if any argument is NULL.
mysql> SELECT RIGHT(‘foobarbar’, 4);
-> ‘rbar’

RPAD(str,len,padstr)

Returns the string str, right-padded with the string padstr to a length of len characters. If str is longer than len, the return value is shortened to len characters.

mysql> SELECT RPAD('hi',5,'?');
        -> 'hi???'
mysql> SELECT RPAD('hi',1,'?');
        -> 'h'

RTRIM(str)

Returns the string str with trailing space characters removed.

mysql> SELECT RTRIM('barbar   ');
        -> 'barbar'

SPACE(N)

Returns a string consisting of N space characters.

mysql> SELECT SPACE(6);
        -> '    

返回一个由N个空格字符组成的字符串。

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. The forms with a len argument return a substring len characters long from string str, starting at position pos. The forms that use FROM are standard SQL syntax. It is also possible to use a negative value for pos. In this case, the beginning of the substring is pos characters from the end of the string, rather than the beginning. A negative value may be used for pos in any of the forms of this function.
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.

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参数的表单从字符串str的pos位置开始返回一个子字符串。
有len参数的表单从字符串str的pos位置开始返回一个子字符串len字符
使用from的表单是标准的SQL语法。
也可以对pos使用负值,在这种情况下,子字符串的开头是字符串末尾的pos字符,而不是开头。
在此函数的任何形式中,pos都可以使用负值。

对于所有形式的SUBSTRING(),要从中提取子字符串的字符串中第一个字符的位置被认为是1。

SUBSTRING_INDEX(str,delim,count)

Returns the substring from string str before count occurrences of the delimiter delim. If count is positive, everything to the left of the final delimiter (counting from the left) is returned. If count is negative, everything to the right of the final delimiter (counting from the right) is returned. SUBSTRING_INDEX() performs a case-sensitive match when searching for delim.

mysql> SELECT SUBSTRING_INDEX('www.mysql.com', '.', 2);
        -> 'www.mysql'
mysql> SELECT SUBSTRING_INDEX('www.mysql.com', '.', -2);
        -> 'mysql.com'

在分隔符delim出现计数之前,从字符串str返回子字符串。
如果count为正数,则返回最后分隔符左边的所有内容(从左边计数)。
如果count为负数,则返回最后分隔符右边的所有内容(从右边计数)。SUBSTRING_INDEX()在搜索delim时执行区分大小写的匹配。

TO_BASE64(str)

Converts the string argument to base-64 encoded form and returns the result as a character string with the connection character set and collation. 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. Base-64 encoded strings can be decoded using the FROM_BASE64() function.
将字符串参数转换为以base-64编码的形式,并使用连接字符集和排序规则以字符串的形式返回结果。如果参数不是字符串,则在转换之前将其转换为字符串。如果参数为空,则结果为空。可以使用FROM_BASE64()函数解码Base-64编码的字符串。

mysql> SELECT TO_BASE64('abc'), FROM_BASE64(TO_BASE64('abc'));
        -> 'JWJj', 'abc'

Different base-64 encoding schemes exist. These are the encoding and decoding rules used by TO_BASE64() and FROM_BASE64():
 The encoding for alphabet value 62 is ‘+’.
 The encoding for alphabet value 63 is ‘/’.
 Encoded output consists of groups of 4 printable characters. Each 3 bytes of the input data are encoded using 4 characters. If the last group is incomplete, it is padded with ‘=’ characters to a length of 4.
 A newline is added after each 76 characters of encoded output to divide long output into multiple lines.
 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. If none of the specifiers BOTH, LEADING, or TRAILING is given, BOTH is assumed. remstr is optional and, if not specified, spaces are removed.

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'

返回删除所有remstr前缀或后缀的字符串str。
如果没有一个说明符同时给出、引导符或尾随符,则假定两者都有。
remstr是可选的,如果没有指定,则删除空格。

UNHEX(str)

For a string argument str, UNHEX(str) interprets each pair of characters in the argument as a hexadecimal number and converts it to the byte represented by the number. The return value is a binary string.
对于字符串参数str, UNHEX(str)将参数中的每对字符解释为十六进制数字,并将其转换为该数字所表示的字节。返回值是一个二进制字符串。

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’. If the argument contains any nonhexadecimal digits, the result is NULL:

参数字符串中的字符必须是合法的十六进制数字:‘0’ …“9”、“‘. .“F”、“‘. .“f”。如果参数包含任何非十六进制数字,则结果为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. 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 ‘A’. 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). ‘\0’ is not a legal hexadecimal digit, so UNHEX() for the column value returns NULL.
For a numeric argument N, the inverse of HEX(N) is not performed by UNHEX(). Use CONV(HEX(N),16,10) instead. See the description of HEX().

UPPER(str)

Returns the string str with all characters changed to uppercase according to the current character set mapping. The default is utf8mb4.

返回字符串str,根据当前字符集映射将所有字符更改为大写。默认为utf8mb4。

mysql> SELECT UPPER('Hej');
        -> 'HEJ'

See the description of LOWER() for information that also applies to UPPER(). 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.
This function is multibyte safe.
UCASE() used within views is rewritten as UPPER().

你可能感兴趣的:(mysql)