开发者

How to find a string in field and add an url before it in mysql database?

开发者 https://www.devze.com 2023-03-25 20:27 出处:网络
I would like to know how to find in a table field all possible instances of a string e.g. uploads/ and add before all of them another string e.g.

I would like to know how to find in a table field all possible instances of a string e.g.

uploads/

and add before all of them another string e.g.

http://example.com/

My idea is to update all my records in database where the old links are not working so for instace I have something like this for a field perex

Check this car <img s开发者_如何学Pythonrc="/uploads/audi.jpg">. It is fantastic!

To look like:

Check this car <img src="http://example.com/uploads/audi.jpg">. It is fantastic!

And I have hundreds of this situations in my db.

What could an update query look like? Assume we have it in one table called cars and field is called perex. We need to find there all instances of /uploads and add before the slash http://example.com/

Thanks in advance for your answer.


UPDATE cars
SET perex = REPLACE(perex, '/uploads/', 'http://example.com/uploads/')
WHERE INSTR(perex, '/uploads/');

See:

  • REPLACE()
  • INSTR()


Try this

Update table
set Perex = Replace (Perex, 'Check this car <img src="', 'Check this car <img src="http://example.com')
Where Perex like '%uploads/%'
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号