mysql数据处理之replace用法详解,加一个线上项目案例分享
数据库  /  管理员 发布于 1年前   760
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
最后来一张图片吧
希望对你有帮助
博主 在
hyperf框架常用命令-在centos7中退出命令及在docker容器中退出命令中评论 @路过的靓仔:cdn静态资源被墙,已修复..GGGGGGGGG 在
layui框架常用输入框介绍中评论 写的很好解决问题..路过的靓仔 在
hyperf框架常用命令-在centos7中退出命令及在docker容器中退出命令中评论 剩下好多 wait 状态的..激光豆芽 在
为什么你不能安逸?国内996为什么没有国外955香?中评论 国内现在无意义的内卷太多了..激光豆芽 在
阿里云香港服务器搭建自用vpn:Shadowsocks使用流程步骤中评论 厉害了..
Copyright·© 2019 侯体宗版权所有·
粤ICP备20027696号