Home > Database > Mysql Tutorial > mysql 中replace()批量替换指定字符语句

mysql 中replace()批量替换指定字符语句

WBOY
Release: 2016-06-07 17:51:20
Original
1133 people have browsed it

本文章介绍了关于一个mysql 中replace()批量替换指定字符语句的,我们利用了update set和replace一起工作,这样就可以实现我们想要的东西了。

将cdb_pms表subject字段中的Welcom to替换成 欢迎光临

 代码如下 复制代码

UPDATE `cdb_pms`
SET `subject` = REPLACE(`subject`, 'Welcome to', '欢迎光临')
WHERE INSTR(`subject`,'Welcome to') > 0

替换cdb_posts表的message字段,将“viewthread.?tid=3989”替换成“viewthread.php?tid=16546”

 代码如下 复制代码

UPDATE `cdb_posts`
SET `message`= REPLACE(`message`, 'viewthread.php?tid=3989', 'viewthread.php?tid=16546')
WHERE INSTR(`message`,'viewthread.php?tid=3989') > 0 ;

 

删除所有的空格

 代码如下 复制代码

UPDATE `es_product`   SET `pro_pub_time` = TRIM(`pro_pub_time`)

删除所有饱含'['或者']'或者'.'的字符

 代码如下 复制代码
UPDATE `es_product`   SET `pro_pub_time` = REPLACE(`pro_pub_time`, '[','')   WHERE INSTR(`pro_pub_time`,'[') > 0
UPDATE `es_product`   SET `pro_pub_time` = REPLACE(`pro_pub_time`, ']','')   WHERE INSTR(`pro_pub_time`,']') > 0
UPDATE `es_product`   SET `pro_pub_time` = REPLACE(`pro_pub_time`, '.','-')   WHERE INSTR(`pro_pub_time`,'.') > 0

 

替换所有含中文'-'的为英文'-'

 代码如下 复制代码
UPDATE `es_product`   SET `pro_pub_time` = REPLACE(`pro_pub_time`, '-','-')   WHERE INSTR(`pro_pub_time`,'-') > 0

将所有的年月都替换成'-'

 代码如下 复制代码

UPDATE `es_product`   SET `pro_pub_time` = REPLACE(`pro_pub_time`, '年','-')   WHERE INSTR(`pro_pub_time`,'年') > 0
UPDATE `es_product`   SET `pro_pub_time` = REPLACE(`pro_pub_time`, '月','-')   WHERE INSTR(`pro_pub_time`,'月') > 0

将所有'2005-04-'这种类型的替换成'2005-04-01'

 代码如下 复制代码

UPDATE `es_product`   SET `pro_pub_time` = CONCAT( `pro_pub_time`, '01')   WHERE SUBSTRING_INDEX( `pro_pub_time`, '-', -1) = '' AND LENGTH(`pro_pub_time`) > 0 AND LENGTH(`pro_pub_time`) > 5

将所有'2005-'这种类型替换成'2005-01-01'

 代码如下 复制代码

UPDATE `es_product`   SET `pro_pub_time` = CONCAT( `pro_pub_time`, '01-01') WHERE INSTR(`pro_pub_time`,'-') > 0 AND   LENGTH(`pro_pub_time`) = 5

将所有 饱含'-',但是位数小于8的改成追加'-01'

 代码如下 复制代码

UPDATE `es_product`   SET `pro_pub_time` = CONCAT( `pro_pub_time`, '-01') WHERE INSTR(`pro_pub_time`,'-') > 0 AND   LENGTH(`pro_pub_time`)

将所有'2005'这样的改成'2005-01-01'

 代码如下 复制代码

UPDATE `es_product`   SET `pro_pub_time` = CONCAT(`pro_pub_time`,'-01-01') WHERE INSTR(`pro_pub_time`,'-') = 0 AND   LENGTH(`pro_pub_time`) = 4

最后将所有'2005-01-01'格式化成'2005年01月'

 代码如下 复制代码

UPDATE `es_product`   SET `pro_pub_time` = DATE_FORMAT(`pro_pub_time`,'%Y年%m月') WHERE INSTR(`pro_pub_time`,'-') > 0

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template