前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >mysql字符串处理函数(二)

mysql字符串处理函数(二)

作者头像
AsiaYe
发布2019-11-06 16:33:53
1.1K0
发布2019-11-06 16:33:53
举报
文章被收录于专栏:DBA随笔DBA随笔
mysql字符串处理函数(二)
字符串处理函数示例
昨天讲了字符串处理函数中的一部分,今天将剩余的一部分再写一下。

1.空格函数space和替换函数replace函数

space函数返回由空格组成的字符串。

replace函数使用方法是replace(s,s1,s2),使用字符串s2替换字符串s中所有的s1。

这里看下例子:

代码语言:javascript
复制
root@localhost:3306 [(none)]>select concat('(',space(6),')');
+--------------------------+
| concat('(',space(6),')') |
+--------------------------+
| (      )                 |
+--------------------------+
1 row in set (0.00 sec)

root@localhost:3306 [(none)]>select replace('xxx.mysql.com','x','w');
+----------------------------------+
| replace('xxx.mysql.com','x','w') |
+----------------------------------+
| www.mysql.com                    |
+----------------------------------+
1 row in set (0.04 sec)

2.比较字符串函数strcmp

strcmp函数的使用方法strcmp(s1,s2)

如果s1和s2相等则返回0,如果s1小于s2,则返回-1,其他情况返回1。

代码语言:javascript
复制
root@localhost:3306 [(none)]>select strcmp('txt','txt2'),strcmp('txt2','txt'),st
rcmp('txt','txt');
+----------------------+----------------------+---------------------+
| strcmp('txt','txt2') | strcmp('txt2','txt') | strcmp('txt','txt') |
+----------------------+----------------------+---------------------+
|                   -1 |                    1 |                   0 |
+----------------------+----------------------+---------------------+
1 row in set (0.06 sec)

3.获取子串的函数substring(s,n,len)

substring(s,n,len)的用法是从字符串s中第n个字符开始,截取len个字符长度作为子串,如下:

代码语言:javascript
复制
root@localhost:3306 [(none)]>select substring('helloworld',5,3),substring('hello
world',5),substring('helloworld',-3),substring('helloworld',-5,4)\G
*************************** 1. row ***************************
 substring('helloworld',5,3): owo
   substring('helloworld',5): oworld
  substring('helloworld',-3): rld
substring('helloworld',-5,4): worl
1 row in set (0.05 sec)

其中,第二种使用方法省略了len值,这个值如果省略,则返回的是从第n个字符开始后面所有的字符串,第三种方法使用了n=-3的方法,说明是从字符串末尾开始数的,直到字符串的结尾,第四种方法则是从末尾倒数第5个字符开始,截取4个字符作为子串。

4匹配子串开始位置的函数

locate、position、instr三个函数的作用相同,都是返回子串在字符串中的位置。来看例子:

代码语言:javascript
复制
root@localhost:3306 [(none)]>select locate('ball','football'),position('ball' in
 'football'),instr('football','ball')\G
*************************** 1. row ***************************
     locate('ball','football'): 5
position('ball' in 'football'): 5
      instr('football','ball'): 5
1 row in set (0.00 sec)

需要注意的是instr函数需要把子串的位置放在后面,其他两个函数子串的位置是放在前面的。

5.字符串逆序的函数

reverse(str),这个比较好理解,这里给出例子:

代码语言:javascript
复制
root@localhost:3306 [(none)]>select reverse('helloworld');
+-----------------------+
| reverse('helloworld') |
+-----------------------+
| dlrowolleh            |
+-----------------------+
1 row in set (0.00 sec)

6.返回指定位置字符串的函数ELT

ELT(n,str0,str1,str2),返回后面那些字符串的第n个,如果越界,则返回null

代码语言:javascript
复制
root@localhost:3306 [(none)]>select elt(3,'1st','2nd','3rd'),elt(3,'1st','2nd');
+--------------------------+--------------------+
| elt(3,'1st','2nd','3rd') | elt(3,'1st','2nd') |
+--------------------------+--------------------+
| 3rd                      | NULL               |
+--------------------------+--------------------+
1 row in set (0.00 sec)

7.返回指定字符串位置的函数field函数

field函数使用方法如下:

field(dst,str1,str2,str3,str4)返回的是后面的字符串中第一个等于dst的字符串的位置,如果不存在,则返回0,示例如下:

代码语言:javascript
复制
root@localhost:3306 [(none)]>select field('hello','I','am','hello'),field('hello
','I','am','a','student');
+---------------------------------+---------------------------------------+
| field('hello','I','am','hello') | field('hello','I','am','a','student') |
+---------------------------------+---------------------------------------+
|                               3 |                                     0 |
+---------------------------------+---------------------------------------+
1 row in set (0.00 sec)

这个函数还有一个变形之后的函数,叫做find_in_set,它代表函数在一个set集合中的位置,使用方法和field一样,举个例子:

代码语言:javascript
复制
root@localhost:3306 [test]>select find_in_set('hello','I,am,hello'),field('hello
','I','am','hello');
+-----------------------------------+---------------------------------+
| find_in_set('hello','I,am,hello') | field('hello','I','am','hello') |
+-----------------------------------+---------------------------------+
|                                 3 |                               3 |
+-----------------------------------+---------------------------------+
1 row in set (0.00 sec)

8.返回子串位置的函数trim(s1 from s2)

看看实例:

代码语言:javascript
复制
root@localhost:3306 [(none)]>select trim('world' from 'helloworld' );
+----------------------------------+
| trim('world' from 'helloworld' ) |
+----------------------------------+
| hello                            |
+----------------------------------+
1 row in set (0.00 sec)

关于字符串的函数大概就这么多了,后续如果还有遗漏,将会进行补充。

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2019-02-02,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 DBA随笔 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档