[英]Update a column value, replacing part of a string
我在 MySQL 数据库中有一个包含以下列的表
[id, url]
网址如下:
http://domain1.example/images/img1.jpg
我想将所有 URL 更新到另一个域
http://domain2.example/otherfolder/img1.jpg
保持文件名不变。
我必须运行什么查询?
UPDATE urls
SET url = REPLACE(url, 'domain1.example/images/', 'domain2.example/otherfolder/')
UPDATE yourtable
SET url = REPLACE(url, 'http://domain1.example/images/', 'http://domain2.example/otherfolder/')
WHERE url LIKE ('http://domain1.example/images/%');
相关文档: http ://dev.mysql.com/doc/refman/5.5/en/string-functions.html#function_replace
尝试这个...
update [table_name] set [field_name] =
replace([field_name],'[string_to_find]','[string_to_replace]');
您需要WHERE子句仅替换符合 WHERE 子句中条件的记录(而不是所有记录)。 您使用%符号表示部分字符串:IE
LIKE ('...//example.com/images/%');
表示所有以"...//example.com/images/"
并在之后有任何内容的记录(这是...的%
)
另一个例子:
LIKE ('%http://example.com/images/%')
这意味着包含"http://example.com/images/"
的所有记录
在字符串的任何部分...
首先,必须检查
SELECT * FROM `university` WHERE course_name LIKE '%&%'
接下来,必须更新
UPDATE university
SET course_name = REPLACE(course_name, '&', '&') WHERE id = 1
结果:工程与技术 =>工程与技术
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.