Update a column value, replacing part of a string

Addev picture Addev · Apr 16, 2012 · Viewed 418.1k times · Source

I have a table with the following columns in a MySQL database

[id, url]

And the urls are like:

 http://domain1.com/images/img1.jpg

I want to update all the urls to another domain

 http://domain2.com/otherfolder/img1.jpg

keeping the name of the file as is.

What's the query must I run?

Answer

Dmytro Shevchenko picture Dmytro Shevchenko · Apr 16, 2012
UPDATE urls
SET url = REPLACE(url, 'domain1.com/images/', 'domain2.com/otherfolder/')