MySQL wildcard replace

匆匆过客 提交于 2019-12-11 02:29:14

问题


Hi i need help with a MySQL query.
Is it possible to do some wildcard replace for a string like the example below?

String:                        example.com/folderone/some/path/to/file.pdf
String:                        example.com/foldertwo/some/path/to/file.pdf
Replace: newsite.com/some/path/to/file.pdf

newsite.com/some/path/to/file.pdf
newsite.com/some/path/to/file.pdf

To remove the folder and change the domain but keep the path. In this case each folder have a different name with a different length.

something like:

update TABLE set COLUMN = replace(COLUMN, 'example.com/%/', 'newsite.com/');

回答1:


Using SUBSTRING_INDEX:

UPDATE table1 
   SET column1 = REPLACE(
         column1, 
         SUBSTRING_INDEX(column1, '/', 2),
         'newsite.com' 
       )
 WHERE column1 LIKE 'example.com/%/'

This should honour your subfolder structure.



来源:https://stackoverflow.com/questions/30728060/mysql-wildcard-replace

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!