SQL substring replacing

故事扮演 提交于 2019-12-11 01:05:07

问题


I am using HeidiSql and I have a database with ~1000 URL's. Example:

index.php?option=com_flexicontent&view=items&cid=283&id=33
index.php?option=com_flexicontent&view=items&cid=421&id=4411
index.php?option=com_flexicontent&view=items&cid=415&id=4375

What I have to do is to replace the cid= with values from 408 to 477 to cid=403

I have made a SQL script like this:

UPDATE jos_menu
SET link = REPLACE(link, "cid=411", 'cid=403')

But how do I change the cid= values 408 to 477, without making 70 REPLACEs?


回答1:


I cant test this but try this

UPDATE jos_menu
SET link = REPLACE(link, 'cid=' + SUBSTRING(@str, CHARINDEX( 'cid=', @str) + 4, 3), 'cid=403')
WHERE Cast(SUBSTRING(@str, CHARINDEX( 'cid=', @str) + 4, 3) as Int) > 407 And Cast(SUBSTRING(@str, CHARINDEX( 'cid=', @str) + 4, 3) as Int) < 478



回答2:


Make a backup of the table or database in phpmyadmin and save it as a .cvs file. Use excel to do the replacements. Than save it as a .cvs file again, and import it to the database back.



来源:https://stackoverflow.com/questions/12390872/sql-substring-replacing

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