如何在字段中查找字符串并在mysql数据库中添加一个url?

时间:2011-08-03 17:42:25

标签: mysql

我想知道如何在表字段中查找字符串的所有可能实例,例如

uploads/

并在所有人之前添加另一个字符串,例如

http://example.com/

我的想法是更新我在旧数据库中的所有记录,其中旧链接不起作用,因此对于一个字段 perex

我有类似的东西
Check this car <img src="/uploads/audi.jpg">. It is fantastic!

看起来像:

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

我的数据库中有数百个这样的情况。

更新查询可能是什么样的?假设我们将其放在一个名为 cars 的表中,而字段称为 perex 。我们需要找到 / uploads 的所有实例,并在斜线 http://example.com/

之前添加

提前感谢您的回答。

2 个答案:

答案 0 :(得分:0)

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

请参阅:

答案 1 :(得分:0)

试试这个

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