mysql 字段处理函数,mysql字符串处理函数

在日常运维的过程中,经常需要写一些脚本来进行一些数据记录处理的操作,例如一个记录中的列,有的是4个(包含空列),有的是5个,这种情况下,需要对其中的某些列值进行处理,在这些脚本中,常常会用到很多关于字符串的函数,今天把这些字符串处理函数简单整理下,后续如果想实现一些脚本,可以直接拿来用。

1.获取字符串字符数和字符串长度的函数

首先弄清楚两个概念,字符串字符数和字符串长度,字符数是指字符串所包含的的字符个数,字符串长度指的是包含的字节个数。char_length是统计字符数的函数,而length是统计字符串长度的函数,下面来看实例:

1root@localhost:3306 [(none)]>select char_length('egg'),char_length('鸡蛋'); 2+--------------------+-----------------------+ 3| char_length('egg') | char_length('鸡蛋')   | 4+--------------------+-----------------------+ 5|                  3 |                     2 | 6+--------------------+-----------------------+ 71 row in set (0.05 sec) 8 9root@localhost:3306 [(none)]>select length('egg'),length('鸡蛋');10+---------------+------------------+11| length('egg') | length('鸡蛋')   |12+---------------+------------------+13|             3 |                6 |14+---------------+------------------+151 row in set (0.00 sec)    可以看到,'鸡蛋'两个字的所占的字符数是2,字节数是6,这是因为汉字在mysql中使用utf8来表示,一个字符包含3个字节,所以结果就是3*2=6

2.合并字符串函数concat和concat_ws

concat函数返回连接参数产生的字符串,可以是两个,可以是多个,如果其中有一个为null,那么返回结果是null:

1root@localhost:3306 [(none)]>select concat('MySQL','5.7'),concat('MySQL',null,'5.7');2+-----------------------+----------------------------+3| concat('MySQL','5.7') | concat('MySQL',null,'5.7') |4+-----------------------+----------------------------+5| MySQL5.7              | NULL                       |6+-----------------------+----------------------------+71 row in set (0.05 sec)   concat_ws函数接受一个分隔符,其他的用法和concat一致,直接来看例子:

1root@localhost:3306 [(none)]>select concat_ws('-','MySQL','5.7'),concat_ws('*','MySQL',null,'5.7');2+------------------------------+-----------------------------------+3| concat_ws('-','MySQL','5.7') | concat_ws('*','MySQL',null,'5.7') |4+------------------------------+-----------------------------------+5| MySQL-5.7                    | MySQL*5.7                         |6+------------------------------+-----------------------------------+71 row in set (0.00 sec)    需要注意的一点是,如果中间遇到null值,那么会直接进行忽略。

3.insert替换字符串函数

insert函数的使用方法为insert(str1,x,len,str2)

也就是使用str2带替换str1中的第x个字符开始后面的len个字符,话不多说,看例子:

1root@localhost:3306 [(none)]>select insert('hello',2,4,'haha'),insert('hello',-12,4,'haha'),insert('hello',2,10,'haha'),insert('hello',2,3,'haha')\G3*************************** 1. row ***************************4 insert('hello',2,4,'haha'): hhaha5insert('hello',-1,4,'haha'): hello6insert('hello',2,10,'haha'): hhaha7 insert('hello',2,3,'haha'): hhahao81 row in set (0.00 sec)上面的例子中,第一个替换的是从第2个字符开始,用4个字符替换hello中的四个字符;第二个是当起始位置变为-1的时候,超越了下标范围,则直接保持hello字符串不变;第三个替换的是从第二个开始,连续10个字符,后面的len越界,字符串只替换到实际中最后一个位置;最后一个替换中间的3个字符,所以总长度增加了1.

4字母大小写转换函数

这个比较好理解,直接看例子:

1root@localhost:3306 [(none)]>select lower('HELLO'),lcase('Hello'),upper('hello')2,ucase('HeLlo')\G3*************************** 1. row ***************************4lower('HELLO'): hello5lcase('Hello'): hello6upper('hello'): HELLO7ucase('HeLlo'): HELLO81 row in set (0.03 sec)

5.截取指定长度的字符串

分别是left函数和right函数,这两个函数也比较好理解,直接上例子:

1root@localhost:3306 [(none)]>select left('helloworld',5),right('helloworld',5);2+----------------------+-----------------------+3| left('helloworld',5) | right('helloworld',5) |4+----------------------+-----------------------+5| hello                | world                 |6+----------------------+-----------------------+71 row in set (0.00 sec)

6.填充字符串的函数LPAD和RPAD

LPAD(s1,len,s2)这个函数是使用s2字符串把s1左侧的字符填充到len长度。

RPAD(s1,len,s2)这个函数是使用s2字符串把s1右侧的字符填充到len长度。

1root@localhost:3306 [(none)]>select lpad('hello',4,'?'),lpad('hello',8,'?') ; 2+---------------------+---------------------+ 3| lpad('hello',4,'?') | lpad('hello',8,'?') | 4+---------------------+---------------------+ 5| hell                | ???hello            | 6+---------------------+---------------------+ 71 row in set (0.04 sec) 8 910root@localhost:3306 [(none)]>select rpad('hello',4,'?'),rpad('hello',8,'?');11+---------------------+---------------------+12| rpad('hello',4,'?') | rpad('hello',8,'?') |13+---------------------+---------------------+14| hell                | hello???            |15+---------------------+---------------------+161 row in set (0.00 sec)    需要注意的是,如果字符串的长度大于中间的len的时候,字符串会被截断到len值。

7.删除空格的函数LTRIM,RTRIM和TRIM

Ltrim是删除左侧的空格,Rtrim是删除右侧空格,trim是删除两侧的空格,示例如下:

1root@localhost:3306 [(none)]>select '(  book  )',concat('(',ltrim('  book  '),')2'),concat('(',rtrim('  book  '),')'),concat('(',trim('  book  '),')')\G3*************************** 1. row ***************************4                       (  book  ): (  book  )5concat('(',ltrim('  book  '),')'): (book  )6concat('(',rtrim('  book  '),')'): (  book)7 concat('(',trim('  book  '),')'): (book)81 row in set (0.00 sec)

8.删除指定字符串的函数trim(s1 from s2)

看看实例:

1root@localhost:3306 [(none)]>select trim('world' from 'helloworld' );2+----------------------------------+3| trim('world' from 'helloworld' ) |4+----------------------------------+5| hello                            |6+----------------------------------+71 row in set (0.00 sec)

9.重复生成字符串的函数repeat

这个比较好理解,直接看例子:

1root@localhost:3306 [(none)]>select repeat('mysql',3) ;2+-------------------+3| repeat('mysql',3) |4+-------------------+5| mysqlmysqlmysql   |6+-------------------+71 row in set (0.00 sec)

还有一部分,明天接着写吧。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值