有時候需要對mysql中的內容進行部分替換,那麼可以參考下面的文章。
將cdb_pms表subject字段中的Welcom to替換成 歡迎光臨
UPDATE `cdb_pms` SET `subject` = REPLACE(`subject`, 'Welcome to', '歡迎光臨') WHERE INSTR(`subject`,'Welcome to') > 0
替換cdb_posts表的message字段,將“viewthread.php?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`) < 8
將所有'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