mysql数据处理之replace用法详解,加一个线上项目案例分享

管理员 发布于 3年前   542

mysql数据处理之replace用法详解,加一个线上项目案例分享


mysql中replace函数直接替换mysql数据库中某字段中的特定字符串,不再需要自己写函数去替换,用起来非常的方便,mysql 替换函数replace()


语法

REPLACE ( string_expression , string_pattern , string_replacement )

即:将string_expression 中所有出现的string_pattern 替换为string_replacement 


参数

string_expression 要搜索的字符串表达式。string_expression 可以是字符或二进制数据类型。

string_pattern 是要查找的子字符串。string_pattern 可以是字符或二进制数据类型。string_pattern 不能是空字符串 ('')。

string_replacement 替换字符串。string_replacement 可以是字符或二进制数据类型。


返回类型

如果其中的一个输入参数数据类型为 nvarchar,则返回 nvarchar;否则 REPLACE 返回 varchar。

如果任何一个参数为 NULL,则返回 NULL。


来个案例

问题:把activedays字段(数字随机) 1,2,3,4,5,6,7改成["1","2","3","4","5","6","7"]


//严重警告,数据处理一定要备份,或者复制一份后在操作

1.根据条件查出来要处理的数据

select id,activedays from lara_55_cbb_NewsList where type=2 and activedays is not null

2.activedays字段两边加[]

update lara_55_cbb_NewsList set activedays=replace(`activedays`,`activedays`,CONCAT('["',`activedays`,'"]')) where type=2 and activedays is not null

3.activedays字段中给逗号加双引号 即:, -> ","

update lara_55_cbb_NewsList set activedays=replace(`activedays`,',','","') where type=2 and activedays is not null

4.activedays字段字符串之间空格去掉  因为原始数据是手动输入的,数字直接有可能有空格

update lara_55_cbb_NewsList set activedays=replace(`activedays`,' ','') where type=2 and activedays is not null

最后来一张图片吧

11.png


希望对你有帮助

请勿发布不友善或者负能量的内容。与人为善,比聪明更重要!

该博客于2020-12-7日,后端基于go语言的beego框架开发
前端页面使用Bootstrap可视化布局系统自动生成

是我仿的原来我的TP5框架写的博客,比较粗糙,底下是入口
侯体宗的博客

      订阅博客周刊

文章标签

友情链接

HouTiZong
侯体宗的博客
© 2020 zongscan.com
版权所有ICP证 : 粤ICP备20027696号
PHP交流群
侯体宗的博客