以下方法有局限性,所以当一个思路和兴趣了解一下就够了,正确的处理办法是把数据取出来处理完成后再更新,mysql语句并不能使用正则进行替换。
UPDATE test set test.data=REPLACE (test.data,SUBSTR(test.data,LOCATE("{",test.data),LOCATE("}",test.data)),"") where 1=1
可以实现将{}之中的值替换为空
例子:data字段里面的数据为
[{"key1":"value1"},{"key2":"value2"}]
那么执行该语句之后data被修改为:
[{"key2":"value2"}]
搜索图片正则表达式:<img.*src=[^>]*>
函数解释:
LOCATE(substr,str) //返回在substr字符串中str第一次出现的位置
SUBSTR(str,pos,len) //返回str字符串中的从第pos位置开始的len个长度字符串。
REPLACE(str1, str2, str3) //将所有str1字符串中出现的str2替换为str3
将comment表中的author_url包含www.sohu.com的记录,其中的sohu替换为sina,一个语句搞定~
update comment set author_url=REPLACE(author_url,'sohu','sina') where author_url REGEXP 'www.sohu.com';
带IF判断的复杂替换
update comment set url=IF(url REGEXP 'test.yahoo.com.cn',REPLACE(url,'www1.sohu.com','www.sina.com'),REPLACE(url,'www2.yahoo.com','www.sina.com')) where 1=1;