2

I want to use MySQL query to change a link .

the link is like this :

http://website.com/click.php?ad_client=blablabla&add_id=548124&more=stuffhere

if I know the add_id number this is easy :

UPDATE table SET name = REPLACE(name, '&add_id=548124', '')

The problem is I have to change 5000 lines and I don't know the add_id number ... so what would be a correct mysql replace() code to remove &add_id=somenumber ??

5 Answers 5

1

USE This....

 UPDATE table 
    SET name = CONCAT(SUBSTRING(name , 1, 
                    INSTR(name ,'&add_id') - 1),SUBSTRING(name , 
                    INSTR(name , '&more'), 
                    LENGTH(name ) - INSTR(name , '&add_id')))
Sign up to request clarification or add additional context in comments.

1 Comment

this looks nice . I will try it now and post back .
0

Either you can do it via UDF - SO Answer or you can simply write PHP code which will replace value & update column again in table.

Comments

0

I would create a stored procedure that uses a cursor to iterate over each row that needs updating.

In the procedure I would find the link to replace and then replace them, one by one.

I've made a sqlfiddle to show how you can get the part to replace inside an select.

I think this approach is clean and easy to read but it's possible to write this a update (that will most likely be hard to read).

Comments

0

And now for your UPDATE:

UPDATE table SET name = REPLACE(name,SUBSTR(name,LOCATE('&',name),LOCATE('&',name,LOCATE('&',name)+1)-LOCATE('&',name)),'')

1 Comment

np, it does the same thing as the first answer but using a different way, you can benchmark both to see which is the fastest for you
0

try this with REPLACE

 UPDATE Table1
 SET name = REPLACE(if(name like '%add_id=%','' , name ), 

 '&add_id=' , '' )

DEMO HERE

2 Comments

not good ... i need to replace &add_id=423423 with '' .. and the number is random .
what result in my demo you want ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.