434

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

[id, url]

And the URLs are like:

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

I want to update all the URLs to another domain

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

keeping the name of the file as is.

What's the query must I run?

Stephen Ostermiller
  • 23,933
  • 14
  • 88
  • 109
Addev
  • 31,819
  • 51
  • 183
  • 302

6 Answers6

919
UPDATE urls
SET url = REPLACE(url, 'domain1.example/images/', 'domain2.example/otherfolder/')
Stephen Ostermiller
  • 23,933
  • 14
  • 88
  • 109
Dmytro Shevchenko
  • 33,431
  • 6
  • 51
  • 67
222
UPDATE yourtable
SET url = REPLACE(url, 'http://domain1.example/images/', 'http://domain2.example/otherfolder/')
WHERE url LIKE ('http://domain1.example/images/%');

relevant docs: http://dev.mysql.com/doc/refman/5.5/en/string-functions.html#function_replace

Stephen Ostermiller
  • 23,933
  • 14
  • 88
  • 109
Marc B
  • 356,200
  • 43
  • 426
  • 500
  • 13
    Hi there- why do I need the where ? – Guy Cohen Oct 07 '14 at 14:21
  • 22
    @GuyCohen Because otherwise the query will modify every single row in the table. The `WHERE` clause optimizes the query to only modify the rows with certain URL. Logically, the result will be the same, but the addition of `WHERE` will make the operation faster. – Dmytro Shevchenko Aug 14 '15 at 14:34
  • 6
    The `WHERE` also ensures that you're only replacing parts of strings that _begin_ with `http://etc/etc/` or `string_to_be_replaced.` For example, in the given answer, `http://domain1.com/images/this/is/a/test` would be affected, but `foobar/http://domain1.com/images/` would not. – Kyle Challis Jan 29 '16 at 19:03
30

Try using the REPLACE function:

mysql> SELECT REPLACE('www.example.com', 'w', 'Ww');
        -> 'WwWwWw.example.com'

Note that it is case sensitive.

Stephen Ostermiller
  • 23,933
  • 14
  • 88
  • 109
schellack
  • 10,144
  • 1
  • 29
  • 33
15

Try this...

update [table_name] set [field_name] = 
replace([field_name],'[string_to_find]','[string_to_replace]');
Stephen Rauch
  • 47,830
  • 31
  • 106
  • 135
ManiMaran A
  • 309
  • 3
  • 6
10

You need the WHERE clause to replace ONLY the records that complies with the condition in the WHERE clause (as opposed to all records). You use % sign to indicate partial string: I.E.

LIKE ('...//example.com/images/%');

means all records that BEGIN with "...//example.com/images/" and have anything AFTER (that's the % for...)

Another example:

LIKE ('%http://example.com/images/%')

which means all records that contains "http://example.com/images/"

in any part of the string...

Stephen Ostermiller
  • 23,933
  • 14
  • 88
  • 109
Kenneth Daly
  • 101
  • 1
  • 2
4

First, have to check

SELECT * FROM `university` WHERE course_name LIKE '%&amp%'

Next, have to update

UPDATE university
SET course_name = REPLACE(course_name, '&amp', '&') WHERE id = 1

Results: Engineering &amp Technology => Engineering & Technology

Peyman Mohamadpour
  • 17,954
  • 24
  • 89
  • 100
Solomon Suraj
  • 1,162
  • 8
  • 8