SQL UPDATE中的str_replace?

时间:2010-10-15 16:04:58

标签: sql mysql string

这是一个示例表:

name       |   picture

John S.    |   http://servera.host.com/johns.png
Linda B.   |   http://servera.host.com/lindab.png
...

假设还有几百条记录。

我们还说我们将服务器从“servera”转移到“serverb”。

是否可以使用一个查询进入此表,以便为每个记录重命名“picture”列中的内容以读取正确的服务器名称?

2 个答案:

答案 0 :(得分:97)

T-SQL:

update TBL 
   set picture = Replace(picture, 'servera', 'serverb') 
 where picture like '%servera%'

甲骨文:

update TBL 
   set picture = replace(picture, 'servera', 'serverb') 
 where picture like '%servera%'

MySQL的:

update TBL 
   set picture = REPLACE(picture, 'servera', 'serverb') 
 where picture like '%servera%'

答案 1 :(得分:18)

UPDATE users
SET picture = REPLACE(picture, 'http://servera.host.com/', 'http://serverb.host.com/')
WHERE picture LIKE 'http://servera.host.com/%';

我要包含更多字符串,因为我担心'修复'名为'somethingserverasomething.jpg'的图像。我也可以考虑使用base_url表并在用户中存储图像文件名,但这不是你问的问题; - )