我有一个表与以下列在MySQL数据库
[id, url]
url是这样的:
http://domain1.example/images/img1.jpg
我想更新所有的url到另一个域
http://domain2.example/otherfolder/img1.jpg
保持文件名称不变。
我必须运行什么查询?
我有一个表与以下列在MySQL数据库
[id, url]
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
尝试使用REPLACE函数:
mysql> SELECT REPLACE('www.example.com', 'w', 'Ww');
-> 'WwWwWw.example.com'
注意,它是区分大小写的。
您只需要使用WHERE子句替换符合WHERE子句条件的记录(而不是所有记录)。您使用%符号表示部分字符串:即
LIKE ('...//example.com/images/%');
表示所有以“…//example.com/images/”开头的记录,并且在后面有任何内容(这是…的%)
另一个例子:
LIKE ('%http://example.com/images/%')
这意味着所有包含“http://example.com/images/”的记录
在字符串的任何部分…
试试这个…
update [table_name] set [field_name] =
replace([field_name],'[string_to_find]','[string_to_replace]');
首先,要检查
SELECT * FROM `university` WHERE course_name LIKE '%&%'
接下来,就要更新了
UPDATE university
SET course_name = REPLACE(course_name, '&', '&') WHERE id = 1
结果:工程技术=>工程技术