SQL Query replace string with placeholder

andy :

My table has lots of entries (image paths). Such a string is an URL and looks like that

https://example.com/images/do.php?id=1234

And i have to change them all to this format

https://example.com/images/1234.png

So the "ID" is equal to the filename. Replacing just the URL isn't that hard, but i have to add the static file extension, which is in my case "png" at the end of the URL string. So i need something like that

UPDATE post SET message = REPLACE(message, 'https://example.com/images/do.php?id=', 'https://example.com/images/{id}.png');

I'm absolutely no experienced SQL user, so can you please help me out?

Gordon Linoff :

This works for the example in your question:

select concat(substring_index(path, 'do.php', 1),
              substring_index(path, '=', -1),
              '.png')
from (select 'https://example.com/images/do.php?id=1234' as path) x

You can easily turn this to an update:

update post
    set message = concat(substring_index(message, 'do.php', 1),
                         substring_index(message, '=', -1),
                         '.png')
    where message like '%do.php%=%';

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=8130&siteId=1