Replace the contents of a database with regular expressions in MySQL

  • 2020-05-06 11:47:03
  • OfStack

PS: the following is turned around to record, this is not the original intention of regular, just use REGEXP, regular more flexible and convenient

author_url in comment table contains the records of www.sohu.com, where sohu is replaced by sina, in one statement ~
update   comment   set   author_url=REPLACE(author_url,'sohu','sina')   where   author_url   REGEXP   'www.sohu.com';

Complex substitution of
with IF judgment
update   comment   set   url=IF(url   REGEXP   'test.yahoo.com.cn',REPLACE(url,'www1.sohu.com','www.sina.com'),REPLACE(url,'www2.yahoo.com','www.sina.com'))   where   1=1;  

Related articles: