mysql中replace替换字符串更改方法

MySQL中update替换部分字符串replace的简单用法

  近日,遇到了需要将部分字符串替换为另外的字符,平时用的最多的是直接update整个字段值,在这种情况下效率比较低,而且容易出错。其实mysql提供了正则表达式中replace这个函数,用起来很简单,特此记录如下:

1、创建测试数据

DROP TABLE IF EXISTS  `activity`;
CREATE TABLE `activity` (
`id` bigint(20) NOT NULL,
`code` varchar(100) NOT NULL COMMENT '活动编号',
`name` varchar(64) NOT NULL COMMENT '活动名称',
`desc` varchar(64) DEFAULT NULL COMMENT '活动描述',
`type` tinyint(4) NOT NULL COMMENT '活动类型:事件活动,分享活动',
`count` int(11) NOT NULL COMMENT '可完成次数',
`auto_receive` int(11) DEFAULT '' COMMENT '是否自动领取奖励',
`show_in_center` int(11) DEFAULT '' COMMENT '是否在任务中心展示',
`reward_gold` int(11) NOT NULL COMMENT '奖励金币个数',
`reward_silver` int(11) NOT NULL COMMENT '奖励银币个数',
`time_unit` varchar(45) NOT NULL COMMENT '活动间隔时间单位',
`time` int(11) NOT NULL COMMENT '参加活动时间间隔',
`priority` int(11) NOT NULL COMMENT '活动优先级',
`event` varchar(100) DEFAULT NULL COMMENT '活动所需事件',
`show_after_activity` bigint(20) DEFAULT NULL COMMENT '在某个活动完成后出现',
`status` tinyint(4) NOT NULL COMMENT '活动是否有效',
`start_time` datetime(3) NOT NULL COMMENT '活动开始时间',
`end_time` datetime(3) NOT NULL COMMENT '活动结束时间',
`create_time` bigint(20) NOT NULL COMMENT '创建时间',
`update_time` bigint(20) NOT NULL COMMENT '更新时间',
`action` varchar(100) DEFAULT NULL COMMENT '活动所需的行动',
`action_details` varchar(1000) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='活动表' ; insert into `activity`(`id`,`code`,`name`,`desc`,`type`,`count`,`auto_receive`,`show_in_center`,`reward_gold`,`reward_silver`,`time_unit`,`time`,`priority`,`event`,`show_after_activity`,`status`,`start_time`,`end_time`,`create_time`,`update_time`,`action`,`action_details`)
values(1,'addFriend','每日:添加好友','添加3位好友',1,3,0,1,100,0,'',0,999990,'addFriendEvent',null,0,'2018-11-17 11:29:21','2022-11-17 11:29:22','','','ADDRESS_BOOK','{"button_desc":"立即添加"}');
insert into `activity`(`id`,`code`,`name`,`desc`,`type`,`count`,`auto_receive`,`show_in_center`,`reward_gold`,`reward_silver`,`time_unit`,`time`,`priority`,`event`,`show_after_activity`,`status`,`start_time`,`end_time`,`create_time`,`update_time`,`action`,`action_details`)
values(2,'friendTalk','和朋友聊天超过 25 句','与朋友聊天,可获得大量金币,聊的越多,赚的越多。与单个朋友聊天超过 25 句话,可获得额外金币!',1,25,1,1,25,0,'',0,130,'talkNtoN',null,1,'2018-11-17 11:29:21','2022-11-17 11:29:22','','','TALK','{"button_desc":"去聊天"}');
insert into `activity`(`id`,`code`,`name`,`desc`,`type`,`count`,`auto_receive`,`show_in_center`,`reward_gold`,`reward_silver`,`time_unit`,`time`,`priority`,`event`,`show_after_activity`,`status`,`start_time`,`end_time`,`create_time`,`update_time`,`action`,`action_details`)
values(3,'addGroup','每日:加入群聊','加入群聊',1,1,0,1,15,0,'',0,999990,'addGroupEvent',null,0,'2018-11-17 11:29:21','2022-11-17 11:29:22','','','ADD_FRIEND_OR_TEAM','{"button_desc":"立即加入"}');
insert into `activity`(`id`,`code`,`name`,`desc`,`type`,`count`,`auto_receive`,`show_in_center`,`reward_gold`,`reward_silver`,`time_unit`,`time`,`priority`,`event`,`show_after_activity`,`status`,`start_time`,`end_time`,`create_time`,`update_time`,`action`,`action_details`)
values(4,'groupTalk','群聊中发言超过 25 句','参与群聊,可获得大量金币,聊的越多,赚的越多。在单个群聊中发言超过 25 次,可获得额外金币!',1,25,1,1,25,0,'',0,140,'newGroupTalkEvent',null,1,'2018-11-17 11:29:21','2022-11-17 11:29:22','','','TALK','{"button_desc":"去聊天"}');
insert into `activity`(`id`,`code`,`name`,`desc`,`type`,`count`,`auto_receive`,`show_in_center`,`reward_gold`,`reward_silver`,`time_unit`,`time`,`priority`,`event`,`show_after_activity`,`status`,`start_time`,`end_time`,`create_time`,`update_time`,`action`,`action_details`)
values(5,'setUpID','设置聊天宝号','朋友可通过搜索“聊天宝号”找到你,快快抢注一个好记的聊天宝号吧!',1,1,1,1,50,0,'',0,60,'setIDEvent',null,1,'2018-11-17 11:29:21','2022-11-17 11:29:22','','','ACCOUNT_AND_SAFE','{"button_desc":"立即设置"}');
insert into `activity`(`id`,`code`,`name`,`desc`,`type`,`count`,`auto_receive`,`show_in_center`,`reward_gold`,`reward_silver`,`time_unit`,`time`,`priority`,`event`,`show_after_activity`,`status`,`start_time`,`end_time`,`create_time`,`update_time`,`action`,`action_details`)
values(6,'bindAlipay','绑定支付宝账号','绑定后可将现金收入提现到支付宝,也可通过支付宝扫码付款、发送聊天红包',1,1,1,1,100,0,'',0,40,'bindAlipayEvent',null,1,'2018-11-17 11:29:21','2022-11-17 11:29:22','','','ACCOUNT_AND_SAFE','{"button_desc":"去绑定"}');
insert into `activity`(`id`,`code`,`name`,`desc`,`type`,`count`,`auto_receive`,`show_in_center`,`reward_gold`,`reward_silver`,`time_unit`,`time`,`priority`,`event`,`show_after_activity`,`status`,`start_time`,`end_time`,`create_time`,`update_time`,`action`,`action_details`)
values(7,'inviteSuccess','新手:首次邀请好友注册','新手:首次邀请好友注册',1,1,0,1,300,0,'',0,999990,'inviteEvent',null,0,'2018-11-17 11:29:21','2022-11-17 11:29:22','','','REDIRECT','{"button_desc":"去邀请","url":"https://point.zidanduanxin.com/invite"}');
insert into `activity`(`id`,`code`,`name`,`desc`,`type`,`count`,`auto_receive`,`show_in_center`,`reward_gold`,`reward_silver`,`time_unit`,`time`,`priority`,`event`,`show_after_activity`,`status`,`start_time`,`end_time`,`create_time`,`update_time`,`action`,`action_details`)
values(8,'shareIncome','晒收益','晒收益',2,1,0,0,100,0,'',0,100000,'',null,1,'2018-11-17 11:29:21','2022-11-17 11:29:22','','','SHARE','{"income":"%d","button_desc":""}');
insert into `activity`(`id`,`code`,`name`,`desc`,`type`,`count`,`auto_receive`,`show_in_center`,`reward_gold`,`reward_silver`,`time_unit`,`time`,`priority`,`event`,`show_after_activity`,`status`,`start_time`,`end_time`,`create_time`,`update_time`,`action`,`action_details`)
values(9,'scanAlipay','扫码支付','在右上角的菜单里使用扫码支付,线下付款成功后可获得金币奖励哦',2,1,0,1,50,0,'',0,91,'',null,1,'2018-11-17 11:29:21','2022-11-17 11:29:22','','','SCAN_ALIPAY','{"button_desc":""}');
insert into `activity`(`id`,`code`,`name`,`desc`,`type`,`count`,`auto_receive`,`show_in_center`,`reward_gold`,`reward_silver`,`time_unit`,`time`,`priority`,`event`,`show_after_activity`,`status`,`start_time`,`end_time`,`create_time`,`update_time`,`action`,`action_details`)
values(10,'treasureBox','完成任务','宝箱任务',1,2,0,0,18,0,'',0,100000,'activityCompleteEvent',-1,1,'2018-11-17 11:29:21','2022-11-17 11:29:22','','','','{"button_desc":"","condition_desc":"还需要完成%s个任务打开宝箱"}');
insert into `activity`(`id`,`code`,`name`,`desc`,`type`,`count`,`auto_receive`,`show_in_center`,`reward_gold`,`reward_silver`,`time_unit`,`time`,`priority`,`event`,`show_after_activity`,`status`,`start_time`,`end_time`,`create_time`,`update_time`,`action`,`action_details`)
values(11,'treasureBox','完成任务','宝箱任务',1,4,0,0,68,0,'',0,100000,'activityCompleteEvent',-1,1,'2018-11-17 11:29:21','2022-11-17 11:29:22','','','','{"button_desc":"","condition_desc":"还需要完成%s个任务打开宝箱"}');
insert into `activity`(`id`,`code`,`name`,`desc`,`type`,`count`,`auto_receive`,`show_in_center`,`reward_gold`,`reward_silver`,`time_unit`,`time`,`priority`,`event`,`show_after_activity`,`status`,`start_time`,`end_time`,`create_time`,`update_time`,`action`,`action_details`)
values(12,'treasureBox','完成任务','宝箱任务',1,6,0,0,88,0,'',0,100000,'activityCompleteEvent',-1,1,'2018-11-17 11:29:21','2022-11-17 11:29:22','','','','{"button_desc":"","condition_desc":"还需要完成%s个任务打开宝箱"}');
insert into `activity`(`id`,`code`,`name`,`desc`,`type`,`count`,`auto_receive`,`show_in_center`,`reward_gold`,`reward_silver`,`time_unit`,`time`,`priority`,`event`,`show_after_activity`,`status`,`start_time`,`end_time`,`create_time`,`update_time`,`action`,`action_details`)
values(13,'treasureBox','完成任务','宝箱任务',1,8,0,0,48,0,'',0,100000,'activityCompleteEvent',-1,1,'2018-11-17 11:29:21','2022-11-17 11:29:22','','','','{"button_desc":"","condition_desc":"还需要完成%s个任务打开宝箱"}');
insert into `activity`(`id`,`code`,`name`,`desc`,`type`,`count`,`auto_receive`,`show_in_center`,`reward_gold`,`reward_silver`,`time_unit`,`time`,`priority`,`event`,`show_after_activity`,`status`,`start_time`,`end_time`,`create_time`,`update_time`,`action`,`action_details`)
values(14,'firstTextMoments','发 1 条文字熟人圈帖子','发送一条纯文字形式的熟人圈帖子可获得金币奖励',3,1,1,1,20,0,'',0,150,'postTextMomentsEvent',null,1,'2018-11-17 11:29:21','2022-11-17 11:29:22','','','MOMENTS_TEXT','{"button_desc":"去完成"}');
insert into `activity`(`id`,`code`,`name`,`desc`,`type`,`count`,`auto_receive`,`show_in_center`,`reward_gold`,`reward_silver`,`time_unit`,`time`,`priority`,`event`,`show_after_activity`,`status`,`start_time`,`end_time`,`create_time`,`update_time`,`action`,`action_details`)
values(15,'firstImageMoments','发 1 条图文熟人圈帖子','发送一条包含图片的熟人圈帖子可获得金币奖励',3,1,1,1,20,0,'',0,160,'postImageMomentsEvent',null,1,'2018-11-17 11:29:21','2022-11-17 11:29:22','','','MOMENTS_TEXT','{"button_desc":"去完成"}');
insert into `activity`(`id`,`code`,`name`,`desc`,`type`,`count`,`auto_receive`,`show_in_center`,`reward_gold`,`reward_silver`,`time_unit`,`time`,`priority`,`event`,`show_after_activity`,`status`,`start_time`,`end_time`,`create_time`,`update_time`,`action`,`action_details`)
values(16,'firstVideoMoments','发 1 条视频熟人圈帖子','发送一条包含视频的熟人圈帖子可获得金币奖励',3,1,1,1,20,0,'',0,170,'postVideoMomentsEvent',null,1,'2018-11-17 11:29:21','2022-11-17 11:29:22','','','MOMENTS_TEXT','{"button_desc":"去完成"}');
insert into `activity`(`id`,`code`,`name`,`desc`,`type`,`count`,`auto_receive`,`show_in_center`,`reward_gold`,`reward_silver`,`time_unit`,`time`,`priority`,`event`,`show_after_activity`,`status`,`start_time`,`end_time`,`create_time`,`update_time`,`action`,`action_details`)
values(17,'likeFriendsMoments','给 3 个熟人圈帖子点赞','给 3 个好友的熟人圈帖子各点赞 1 次即可获得金币奖励,点赞自己发的帖子不算哦',3,3,1,1,10,0,'',0,999990,'likeFriendsMomentsEvent',null,0,'2018-11-17 11:29:21','2022-11-17 11:29:22','','','MOMENTS','{"button_desc":"去完成"}');
insert into `activity`(`id`,`code`,`name`,`desc`,`type`,`count`,`auto_receive`,`show_in_center`,`reward_gold`,`reward_silver`,`time_unit`,`time`,`priority`,`event`,`show_after_activity`,`status`,`start_time`,`end_time`,`create_time`,`update_time`,`action`,`action_details`)
values(18,'commentFriendsMoments','给 3 个朋友评论','给 3 个朋友的熟人圈帖子各评论 1 次即可获得金币奖励,评论自己发的帖子不算哦',3,3,1,1,15,0,'',0,180,'commentFriendsMomentsEvent',null,1,'2018-11-17 11:29:21','2022-11-17 11:29:22','','','MOMENTS','{"button_desc":"去完成"}');
insert into `activity`(`id`,`code`,`name`,`desc`,`type`,`count`,`auto_receive`,`show_in_center`,`reward_gold`,`reward_silver`,`time_unit`,`time`,`priority`,`event`,`show_after_activity`,`status`,`start_time`,`end_time`,`create_time`,`update_time`,`action`,`action_details`)
values(19,'momentsBeLiked','被 5 个朋友点赞','自己发的熟人圈帖子,被 5 个以上的好友点赞,即可获得金币奖励',1,5,0,1,30,0,'',0,999990,'momentsBeLikedEvent',null,0,'2018-11-17 11:29:21','2022-11-17 11:29:22','','','MOMENTS','{"button_desc":"去完成"}');
insert into `activity`(`id`,`code`,`name`,`desc`,`type`,`count`,`auto_receive`,`show_in_center`,`reward_gold`,`reward_silver`,`time_unit`,`time`,`priority`,`event`,`show_after_activity`,`status`,`start_time`,`end_time`,`create_time`,`update_time`,`action`,`action_details`)
values(20,'momentsBeCommented','被 5 个朋友评论','自己发的熟人圈帖子,被 5 个以上的朋友评论,即可获得金币奖励,回复朋友评论还可能获得额外奖励',1,5,1,1,30,0,'',0,120,'momentsBeCommentedEvent',null,1,'2018-11-17 11:29:21','2022-11-17 11:29:22','','','MOMENTS','{"button_desc":"去完成"}');
insert into `activity`(`id`,`code`,`name`,`desc`,`type`,`count`,`auto_receive`,`show_in_center`,`reward_gold`,`reward_silver`,`time_unit`,`time`,`priority`,`event`,`show_after_activity`,`status`,`start_time`,`end_time`,`create_time`,`update_time`,`action`,`action_details`)
values(21,'inviteSmsFriend','邀请 1 个通讯录朋友','首次通过通讯录邀请朋友注册聊天宝,不仅能够获得邀请红包,更能获得 150 金币哦',1,1,1,1,150,0,'',0,0,'smsInviteEvent',null,1,'2018-11-17 11:29:21','2022-11-17 11:29:22','','','REDIRECT','{"button_desc":"去邀请","url":"https://point.zidanduanxin.com/invite"}');
insert into `activity`(`id`,`code`,`name`,`desc`,`type`,`count`,`auto_receive`,`show_in_center`,`reward_gold`,`reward_silver`,`time_unit`,`time`,`priority`,`event`,`show_after_activity`,`status`,`start_time`,`end_time`,`create_time`,`update_time`,`action`,`action_details`)
values(22,'inviteQQFriend','邀请 1 个 QQ 朋友','首次通过 QQ 邀请朋友注册聊天宝,不仅能够获得邀请红包,更能获得 100 金币哦',1,1,1,1,100,0,'',0,10,'qqInviteEvent',null,1,'2018-11-17 11:29:21','2022-11-17 11:29:22','','','REDIRECT','{"button_desc":"去邀请","url":"https://point.zidanduanxin.com/invite"}');
insert into `activity`(`id`,`code`,`name`,`desc`,`type`,`count`,`auto_receive`,`show_in_center`,`reward_gold`,`reward_silver`,`time_unit`,`time`,`priority`,`event`,`show_after_activity`,`status`,`start_time`,`end_time`,`create_time`,`update_time`,`action`,`action_details`)
values(23,'inviteWechatFriend','邀请 1 个微信朋友','首次通过微信邀请朋友注册聊天宝,不仅能够获得随机邀请红包,更能获得 100 金币哦',1,1,1,1,100,0,'',0,20,'wechatInviteEvent',null,1,'2018-11-17 11:29:21','2022-11-17 11:29:22','','','REDIRECT','{"button_desc":"去邀请","url":"https://point.zidanduanxin.com/invite"}');
insert into `activity`(`id`,`code`,`name`,`desc`,`type`,`count`,`auto_receive`,`show_in_center`,`reward_gold`,`reward_silver`,`time_unit`,`time`,`priority`,`event`,`show_after_activity`,`status`,`start_time`,`end_time`,`create_time`,`update_time`,`action`,`action_details`)
values(24,'setReadName','昵称修改为真实姓名','将昵称设置为真实姓名更容易朋友认出来哦,快去完成任务领取金币吧',1,1,1,1,100,0,'',0,30,'setNameEvent',null,1,'2018-11-17 11:29:21','2022-11-17 11:29:22','','','PERSONAL_INFO','{"button_desc":"去修改"}');
insert into `activity`(`id`,`code`,`name`,`desc`,`type`,`count`,`auto_receive`,`show_in_center`,`reward_gold`,`reward_silver`,`time_unit`,`time`,`priority`,`event`,`show_after_activity`,`status`,`start_time`,`end_time`,`create_time`,`update_time`,`action`,`action_details`)
values(25,'setRealAvatar','设置真人头像','设置真人头像更容易被朋友认出来哦,快去完成任务领取金币吧',1,1,1,1,50,0,'',0,50,'setAvatarEvent',null,1,'2018-11-17 11:29:21','2022-11-17 11:29:22','','','PERSONAL_INFO','{"button_desc":"去设置"}');
insert into `activity`(`id`,`code`,`name`,`desc`,`type`,`count`,`auto_receive`,`show_in_center`,`reward_gold`,`reward_silver`,`time_unit`,`time`,`priority`,`event`,`show_after_activity`,`status`,`start_time`,`end_time`,`create_time`,`update_time`,`action`,`action_details`)
values(26,'setProfileInfo','完善个人信息','完善你的性别、地区、签名,能让你的朋友更加了解你哦!全部填完才能领取奖励',1,1,1,1,50,0,'',0,70,'setInfoEvent',null,1,'2018-11-17 11:29:21','2022-11-17 11:29:22','','','PERSONAL_INFO','{"button_desc":"立即设置"}');
insert into `activity`(`id`,`code`,`name`,`desc`,`type`,`count`,`auto_receive`,`show_in_center`,`reward_gold`,`reward_silver`,`time_unit`,`time`,`priority`,`event`,`show_after_activity`,`status`,`start_time`,`end_time`,`create_time`,`update_time`,`action`,`action_details`)
values(27,'firstRead','首次阅读 1 篇新闻','在新闻页面选择 1 篇感兴趣的新闻并获得 1 次阅读奖励,即可额外获得 50 金币奖励',1,1,1,1,50,0,'',0,80,'firstReadEvent',null,1,'2018-11-17 11:29:21','2022-11-17 11:29:22','','','NEWS','{"button_desc":"去阅读"}');
insert into `activity`(`id`,`code`,`name`,`desc`,`type`,`count`,`auto_receive`,`show_in_center`,`reward_gold`,`reward_silver`,`time_unit`,`time`,`priority`,`event`,`show_after_activity`,`status`,`start_time`,`end_time`,`create_time`,`update_time`,`action`,`action_details`)
values(28,'firstMoments','发送 1 条熟人圈帖子','发送 1 条熟人圈帖子即可获得额外金币,文字、图片、小视频都可以',1,1,1,1,50,0,'',0,90,'firstMomentsEvent',null,1,'2018-11-17 11:29:21','2022-11-17 11:29:22','','','MOMENTS_TEXT','{"button_desc":"去完成"}');
insert into `activity`(`id`,`code`,`name`,`desc`,`type`,`count`,`auto_receive`,`show_in_center`,`reward_gold`,`reward_silver`,`time_unit`,`time`,`priority`,`event`,`show_after_activity`,`status`,`start_time`,`end_time`,`create_time`,`update_time`,`action`,`action_details`)
values(29,'dailyRead','阅读新闻 20 分钟','每天阅读新闻 20 分钟,除了阅读过程中的奖励,还可获得额外金币奖励!',1,1200,1,1,30,0,'',0,100,'readEvent',null,1,'2018-11-17 11:29:21','2022-11-17 11:29:22','','','NEWS','{"button_desc":"去完成"}');

2、查询name、desc中含有好友的记录

mysql> select id,name,`desc`,update_time from activity where name regexp '好友' or desc regexp '好友';
+------+-----------------------------------+--------------------------------------------------------------------------------------------------------------+---------------+
| id | name | desc | update_time |
+------+-----------------------------------+--------------------------------------------------------------------------------------------------------------+---------------+
| 1 | 每日:添加好友 | 添加3位好友 | 1542454142000 |
| 7 | 新手:首次邀请好友注册 | 新手:首次邀请好友注册 | 1543327524000 |
| 17 | 给 3 个熟人圈帖子点赞 | 给 3 个好友的熟人圈帖子各点赞 1 次即可获得金币奖励,点赞自己发的帖子不算哦 | 1543327524000 |
| 19 | 被 5 个朋友点赞 | 自己发的熟人圈帖子,被 5 个以上的好友点赞,即可获得金币奖励 | 1543327524000 |
+------+-----------------------------------+--------------------------------------------------------------------------------------------------------------+---------------+
4 rows in set (0.01 sec)

3、用update 含有 replace函数进行修改

-- 将name和desc中的好友字符串替换成朋友
mysql> update activity set name=replace(name,'好友','朋友'), `desc`=replace(`desc`,'好友','朋友') where name regexp '好友' or desc regexp '好友';
Query OK, 4 rows affected (0.03 sec)

4、再次查询结果验证

mysql> select id,name,`desc`,update_time from activity where name regexp '好友' or desc regexp '好友';
Empty set (0.01 sec)
mysql> select id,name,`desc`,update_time from activity where id in (1,7,17,19);
+------+-----------------------------------+--------------------------------------------------------------------------------------------------------------+---------------+
| id | name | desc | update_time |
+------+-----------------------------------+--------------------------------------------------------------------------------------------------------------+---------------+
| 1 | 每日:添加朋友 | 添加3位朋友 | 1542454142000 |
| 7 | 新手:首次邀请朋友注册 | 新手:首次邀请朋友注册 | 1543327524000 |
| 17 | 给 3 个熟人圈帖子点赞 | 给 3 个朋友的熟人圈帖子各点赞 1 次即可获得金币奖励,点赞自己发的帖子不算哦 | 1543327524000 |
| 19 | 被 5 个朋友点赞 | 自己发的熟人圈帖子,被 5 个以上的朋友点赞,即可获得金币奖励 | 1543327524000 |
+------+-----------------------------------+--------------------------------------------------------------------------------------------------------------+---------------+
4 rows in set (0.01 sec)
上一篇:mySQL中replace的用法


下一篇:RealProxy实现AOP编程(2)