MySQLstringreplace
我有一个包含url(id,url)的列:
http://www.example.com/articles/updates/43 http://www.example.com/articles/updates/866 http://www.example.com/articles/updates/323 http://www.example.com/articles/updates/seo-url http://www.example.com/articles/updates/4?something=test 我想将“更新”一词改为“新闻”。 是否可以用脚本来做到这一点?
 UPDATE your_table SET your_field = REPLACE(your_field, 'articles/updates/', 'articles/news/') WHERE your_field LIKE '%articles/updates/%' 
是的,MySQL有一个REPLACE()函数:
 mysql> SELECT REPLACE('www.mysql.com', 'w', 'Ww'); -> 'WwWwWw.mysql.com' 
http://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_replace
 请注意,如果在使用SELECT时使其成为别名, SELECT容易 
 SELECT REPLACE(string_column, 'search', 'replace') as url.... 
replace函数应该为你工作。
 REPLACE(str,from_str,to_str) 
 返回stringstr,其中出现的所有stringfrom_str被stringto_strreplace。 searchfrom_str时, REPLACE()执行区分大小写的匹配。 
  除了gmaggio的回答,如果你需要dynamicREPLACE和UPDATE根据另一列你可以做,例如: 
 UPDATE your_table t1 INNER JOIN other_table t2 ON t1.field_id = t2.field_id SET t1.your_field = IF(LOCATE('articles/updates/', t1.your_field) > 0, REPLACE(t1.your_field, 'articles/updates/', t2.new_folder), t1.your_field) WHERE... 
 在我的示例中,stringarticles/news/存储在other_table t2并且不需要在WHERE子句中使用LIKE 。