博客
关于我
mysql函数汇总之字符串函数
阅读量:789 次
发布时间:2023-02-11

本文共 6796 字,大约阅读时间需要 22 分钟。

MySQL 字符串函数指南

MySQL中的字符串函数是处理数据库中的字符串数据的重要工具。这些函数涵盖了字符串长度计算、合并、替换、比较、查找以及格式转换等多种操作。以下是常用的MySQL字符串函数及其使用方法。

1. 计算字符串字符数的函数

CHAR_LENGTH(str)

返回字符串str所包含的字符个数。一个多字节字符算作一个单字符。

示例:

select char_length('date'), char_length('yunweijia'), char_length('运维家');

结果:| char_length('date') | char_length('yunweijia') | char_length('运维家') ||--------------------|-------------------------|---------------------|| 4 | 9 | 3 |

2. 计算字符串长度的函数

LENGTH(str)

返回字符串的字节长度。UTF-8编码下,一个汉字占3字节,数字或字母占1字节。

示例:

select length('date'), length('yunweijia'), length('运维家');

结果:| length('date') | length('yunweijia') | length('运维家') ||----------------|---------------------|-----------------|| 4 | 9 | 9 |

3. 合并字符串函数 CONCATCONCAT_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 |

4. 替换字符串的函数 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 |

5. 字母大小写转换函数

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 |

6. 获取指定长度的字符串的函数 LEFTRIGHT

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 |

7. 填充字符串的函数 LPADRPAD

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@#@#@#@#@#@ |

8. 删除空格的函数 LTRIMRTRIMTRIM

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

9. 删除指定字符串的函数 TRIM

TRIM(s1 FROM s)

删除s中两端的s1子字符串。如果s1未指定,删除空格。

示例:

select trim('@#' from '@#@#yunwei@#jia@#@#');

结果:| trim('@#' from '@#@#yunwei@#jia@#@#') ||---------------------------------------|| yunwei@#jia |

10. 重复生成字符串的函数 REPEAT

REPEAT(s, n)

重复字符串s,生成ns的字符串。如果n <= 0,返回空字符串。

示例:

select repeat('x', 10), repeat('x', 0), repeat('x', null);

结果:| repeat('x', 10) | repeat('x', 0) | repeat('x', null) ||-----------------|---------------|-----------------|| xxxxxxxxxx | | NULL |

11. 空格函数 SPACE(n)

SPACE(n)

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

示例:

select concat('(', space(10), ')');

结果:| concat('(', space(10), ')') ||-----------------------------|| ( ) |

12. 替换函数 REPLACE

REPLACE(s, s1, s2)

s2替代s中所有s1子字符串。

示例:

select replace('yunweijia', 'i', '@#');

结果:| replace('yunweijia', 'i', '@#') ||---------------------------------|| yunwe@#j@#a |

13. 比较字符串大小的函数 strcmp

strcmp(s1, s2)

比较两个字符串的大小。返回0表示相同,-1表示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 |

14. 获取子串的函数 SUBSTRINGMID

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功能相同。

15. 匹配子串开始位置的函数 locatepositioninstr

locate(str1, str)

返回str1str中的开始位置。

示例:

select locate('wei', 'yunweijia'), position('wei' in 'yunweijia'), instr('yunweijia', 'wei');

结果:| locate('wei', 'yunweijia') | position('wei' in 'yunweijia') | instr('yunweijia', 'wei') ||----------------------------|--------------------------------|---------------------------|| 4 | 4 | 4 |

16. 字符串逆序的函数 REVERSE

REVERSE(s)

将字符串s反转。

示例:

select reverse('yunweijia') as coll, reverse('123456789') as coll1;

结果:| coll | coll1 ||-----------|-----------|| aijiewnuy | 987654321 |

17. 返回指定位置的字符串的函数 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 |

18. 返回指定字符串位置的函数 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 |

19. 返回子串位置的函数 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 |

20. 选取字符串的函数 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/

你可能感兴趣的文章
mysqldump 导出数据库中每张表的前n条
查看>>
mysqldump: Got error: 1044: Access denied for user ‘xx’@’xx’ to database ‘xx’ when using LOCK TABLES
查看>>
Mysqldump参数大全(参数来源于mysql5.5.19源码)
查看>>
mysqldump备份时忽略某些表
查看>>
mysqldump实现数据备份及灾难恢复
查看>>
mysqldump数据库备份无法进行操作只能查询 --single-transaction
查看>>
mysqldump的一些用法
查看>>
mysqli
查看>>
MySQLIntegrityConstraintViolationException异常处理
查看>>
mysqlreport分析工具详解
查看>>
MySQLSyntaxErrorException: Unknown error 1146和SQLSyntaxErrorException: Unknown error 1146
查看>>
Mysql_Postgresql中_geometry数据操作_st_astext_GeomFromEWKT函数_在java中转换geometry的16进制数据---PostgreSQL工作笔记007
查看>>
mysql_real_connect 参数注意
查看>>
mysql_secure_installation初始化数据库报Access denied
查看>>
MySQL_西安11月销售昨日未上架的产品_20161212
查看>>
Mysql——深入浅出InnoDB底层原理
查看>>
MySQL“被动”性能优化汇总
查看>>
MySQL、HBase 和 Elasticsearch:特点与区别详解
查看>>
MySQL、Redis高频面试题汇总
查看>>
MYSQL、SQL Server、Oracle数据库排序空值null问题及其解决办法
查看>>