本文共 6796 字,大约阅读时间需要 22 分钟。
MySQL中的字符串函数是处理数据库中的字符串数据的重要工具。这些函数涵盖了字符串长度计算、合并、替换、比较、查找以及格式转换等多种操作。以下是常用的MySQL字符串函数及其使用方法。
CHAR_LENGTH(str)
str
所包含的字符个数。一个多字节字符算作一个单字符。 示例:
select char_length('date'), char_length('yunweijia'), char_length('运维家');
结果:| char_length('date') | char_length('yunweijia') | char_length('运维家') ||--------------------|-------------------------|---------------------|| 4 | 9 | 3 |
LENGTH(str)
示例:
select length('date'), length('yunweijia'), length('运维家');
结果:| length('date') | length('yunweijia') | length('运维家') ||----------------|---------------------|-----------------|| 4 | 9 | 9 |
CONCAT
和 CONCAT_WS
CONCAT(s1, s2, ...)
NULL
,返回NULL
。 示例:
select concat('mysql 8.0', 'yunweijia'), concat('heihei', null, '666');
结果:| concat('mysql 8.0', 'yunweijia') | concat('heihei', null, '666') ||--------------------------------|---------------------------------|| mysql 8.0yunweijia | NULL |
CONCAT_WS(x, s1, s2, ...)
x
作为分隔符。若分隔符为NULL
,返回NULL
。 示例:
select concat_ws('-', 'mysql', '8.0', 'ywj'), concat_ws('@', 'one', null, 'two');
结果:| concat_ws('-', 'mysql', '8.0', 'ywj') | concat_ws('@', 'one', null, 'two') ||-------------------------------------|------------------------------------|| mysql-8.0-ywj | one@two |
INSERT
INSERT(s1, x, len, s2)
s2
替换s1
中从位置x
开始的len
个字符。 示例:
select insert('yunweijia', 2, 3, 'aaaaaaaa') as coll, insert('yunweijia', -1, 3, 'bbbbbbbb') as coll2, insert('yunweijia', 2, 99, 'ccccccccc') as coll3;
结果:| coll | coll2 | coll3 ||----------------|-----------|------------|| yaaaaaaaaeijia | yunweijia | yccccccccc |
LOWER(str)
或 LCASE(str)
示例:
select lower('YUNweiJIA'), lcase('YUNweiJIA');
结果:| lower('YUNweiJIA') | lcase('YUNweiJIA') ||--------------------|---------------------|| yunweijia | yunweijia |
UPPER(str)
或 UCASE(str)
示例:
select upper('yunWEIjia'), ucase('yunWEIjia');
结果:| upper('yunWEIjia') | ucase('yunWEIjia') ||--------------------|---------------------|| YUNWEIJIA | YUNWEIJIA |
LEFT
和 RIGHT
LEFT(s, n)
s
开始的最左边的n
个字符。 示例:
select left('yunweijia', 5);
结果:| left('yunweijia', 5) ||--------------------|| yunwe |
RIGHT(s, n)
s
开始的最右边的n
个字符。 示例:
select right('yunweijia', 5);
结果:| right('yunweijia', 5) ||-----------------------|| eijia |
LPAD
和 RPAD
LPAD(s1, len, s2)
s1
左边填充len
个字符,使用s2
填充。如果s1
长度大于len
,则截断。 示例:
select lpad('yunweijia', 4, '@#'), lpad('yunweijia', 20, '@#');
结果:| lpad('yunweijia', 4, '@#') | lpad('yunweijia', 20, '@#') ||----------------------------|-----------------------------|| yunw | @#@#@#@#@#@yunweijia |
RPAD(s1, len, s2)
s1
右边填充len
个字符,使用s2
填充。如果s1
长度大于len
,则截断。 示例:
select rpad('yunweijia', 4, '@#'), rpad('yunweijia', 20, '@#');
结果:| rpad('yunweijia', 4, '@#') | rpad('yunweijia', 20, '@#') ||----------------------------|-----------------------------|| yunw | yunweijia@#@#@#@#@#@ |
LTRIM
、RTRIM
和 TRIM
LTRIM(s)
s
左边的空格字符。 示例:
select '( yunweijia )', concat('(', ltrim(' yunweijia '), ')');
结果:| ( yunweijia ) | (yunweijia ) |
RTRIM(s)
s
右边的空格字符。 示例:
select '( yunweijia )', concat( '(', rtrim(' yunweijia '), ')' );
结果:| ( yunweijia ) | ( yunweijia) |
TRIM(s)
s
两边的空格字符。 示例:
select '( yunweijia )', concat( '(', trim(' yunweijia '), ')' );
结果:| ( yunweijia ) | (yunweijia) |
TRIM
TRIM(s1 FROM s)
s
中两端的s1
子字符串。如果s1
未指定,删除空格。 示例:
select trim('@#' from '@#@#yunwei@#jia@#@#');
结果:| trim('@#' from '@#@#yunwei@#jia@#@#') ||---------------------------------------|| yunwei@#jia |
REPEAT
REPEAT(s, n)
s
,生成n
个s
的字符串。如果n <= 0
,返回空字符串。 示例:
select repeat('x', 10), repeat('x', 0), repeat('x', null);
结果:| repeat('x', 10) | repeat('x', 0) | repeat('x', null) ||-----------------|---------------|-----------------|| xxxxxxxxxx | | NULL |
SPACE(n)
SPACE(n)
n
个空格组成的字符串。 示例:
select concat('(', space(10), ')');
结果:| concat('(', space(10), ')') ||-----------------------------|| ( ) |
REPLACE
REPLACE(s, s1, s2)
s2
替代s
中所有s1
子字符串。 示例:
select replace('yunweijia', 'i', '@#');
结果:| replace('yunweijia', 'i', '@#') ||---------------------------------|| yunwe@#j@#a |
strcmp
strcmp(s1, s2)
s1
小于s2
,1表示s1
大于s2
。 示例:
select strcmp('txt', 'txt'), strcmp('txt', 'txt2'), strcmp('txt2', 'txt');
结果:| strcmp('txt', 'txt') | strcmp('txt', 'txt2') | strcmp('txt2', 'txt') ||--------------------|-----------------------|-----------------------|| 0 | -1 | 1 |
SUBSTRING
和 MID
SUBSTRING(s, n, len)
s
中返回从位置n
开始的len
个字符。如果n
为负数,从末尾开始。 示例:
select substring('yunweijia', 5, 2) as coll, substring('yunweijia', -3) as coll1, substring('yunweijia', 3) as coss2;
结果:| coll | coll1 | coss2 ||------|-------|---------|| ei | jia | nweijia |
MID(s, n, len)
substring
功能相同。 locate
、position
和 instr
locate(str1, str)
str1
在str
中的开始位置。 示例:
select locate('wei', 'yunweijia'), position('wei' in 'yunweijia'), instr('yunweijia', 'wei');
结果:| locate('wei', 'yunweijia') | position('wei' in 'yunweijia') | instr('yunweijia', 'wei') ||----------------------------|--------------------------------|---------------------------|| 4 | 4 | 4 |
REVERSE
REVERSE(s)
s
反转。 示例:
select reverse('yunweijia') as coll, reverse('123456789') as coll1;
结果:| coll | coll1 ||-----------|-----------|| aijiewnuy | 987654321 |
elt
elt(n, str1, str2, ..., strn)
n
个字符串。如果n
无效,返回NULL
。 示例:
select elt(1, 'ni1', 'wo1', 'ta') as coll, elt(3, 'ni1', 'wo1', 'ta') as coll_1, elt(0, 'ni1', 'wo1', 'ta') as coll_2, elt(4, 'ni1', 'wo1', 'ta') as coll_3;
结果:| coll | coll_1 | coll_2 | coll_3 ||------|--------|--------|--------|| ni1 | ta | NULL | NULL |
FIELD
FIELD(s, s1, s2, ..., sn)
s
在列表s1, s2, ..., sn
中第一次出现的位置。如果找不到s
,返回0。 示例:
select field('w', 'yun', 'yunwei', 'w', 'yunweijia') as coll, field('x', 'yun', 'yunwei', 'w', 'yunweijia') as coll_1, field(null, 'yun', 'yunwei', 'w', 'yunweijia') as coll_2;
结果:| coll | coll_1 | coll_2 ||------|--------|--------|| 3 | 0 | 0 |
FIND_IN_SET
FIND_IN_SET(s1, s2)
s1
在字符串列表s2
中的位置。列表由多个用逗号分隔的字符串组成。如果s1
不在s2
中或s2
为空,返回0。 示例:
select find_in_set('X', 'yun,xwei,Xwei,X,jia');
结果:| find_in_set('X', 'yun,xwei,Xwei,X,jia') ||-----------------------------------------|| 4 |
MAKE_SET
MAKE_SET(x, s1, s2, ..., sn)
x
从列表s1, s2, ..., sn
中选择字符。二进制数从右往左计数。 示例:
select make_set(5, 'a', 'b', 'c', 'd') as coll, make_set(1|4, 'a', 'b', 'c', 'd') as coll_1, make_set(1|4, 'a', 'b', null, 'c', 'd') as coll_2, make_set(0, 'a', 'b', 'c', 'd') as coll_3;
结果:| coll | coll_1 | coll_2 | coll_3 ||------|--------|--------|--------|| a,c | a,c | a | |
以上是MySQL字符串函数的常用功能和示例,希望对您有所帮助!
转载地址:http://eobfk.baihongyu.com/