[英]SQL: Find and Replace with SQL?
我有一个MySQL InnoDB数据库。
我的“ article
”表中的列名为url
,需要更新。
存储在article.url =
/blog/2010/article-name
/blog/1998/the-article-name
/blog/...
我需要将/blog/
更改为/news/
。 (例如现在article.url ='/ news / ...')
将article.url列中的“ / blog /”替换为“ / news /”需要什么SQL?
update url
set article = replace(article, '/blog/', '/news/')
where article like '/blog/%'
如果每个网址都以“ / blog /”开头,并且除了前缀之外,您不想更改其他任何内容,则可以只使用substring()和concat()而不是replace():
update article
set url = concat('/news/',substring(url,7))
where url like '/blog/%';
我最近想即时替换MySQL中的字符串,但是该字段可以包含2个项目。 因此,我将REPLACE()
包装在REPLACE()
,例如:
REPLACE(REPLACE(field_name, “what we are looking for”, “replace first instance”),
“something else we are looking for”, “replace second instance”)
这是我用来检测布尔值的语法:
REPLACE(REPLACE(field, 1, “Yes”), 0, “No”)
希望这可以帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.