需求:更新url中最一个字符的'-1'改为'-5',前面的内容保持不变

url列的内容如下:http://h5game.ecs.cedarmg.com/a/captal/dispther.do?deviceid=b-01-1

SELECT * from qrmsgWHERE url LIKE '%1'

http://h5game.ecs.cedarmg.com/c/captal/dispther.do?deviceid=b-01-1

 

需要把最后这个-1改成-5

 

操作方式如下:

UPDATE qrmsg SET url= CONCAT(substring_index(url,'-','2'),'-5') WHERE url LIKE '%1'

 

 

SELECT* from qrmsg WHERE url LIKE '%5'

http://h5game.ecs.cedarmg.com/ca/capital/dispather.do?deviceid=b-01-5


利用的函数如下:

1substring_index(str,delim,count)

      str:要处理的字符串

      delim:分隔符

      count:计数

 

例子:str=www.google.com

     substring_index(str,'.',1)

      结果是:www

     substring_index(str,'.',2)

      结果是:www.google

      也就是说,如果count是正数,那么就是从左往右数,第N个分隔符的左边的全部内容

      相反,如果是负数,那么就是从右边开始数,第N个分隔符右边的所有内容,如:

     substring_index(str,'.',-2)

      结果为:google.com

     有人会为,如果我呀中间的的google怎么办?

     很简单的,两个方向:

     1、从右数第二个分隔符的右边全部,再从左数的第一个分隔符的左边:

substring_index(substring_index(str,'.',-2),.,1);

     2、你懂得!

 

2concat是连接几个字符串,可以多个哦。

    concat('wo','lin','xue','bin')

     结果就是wolinxuebin



参考blog:

http://blog.csdn.net/wolinxuebin/article/details/7845917 

posted on 2016-08-19 17:13  岁伏  阅读(2215)  评论(0编辑  收藏  举报