[英]Update a column value, replacing part of a string
I have a table with the following columns in a MySQL database我在 MySQL 数据库中有一个包含以下列的表
[id, url]
And the URLs are like:网址如下:
http://domain1.example/images/img1.jpg
I want to update all the URLs to another domain我想将所有 URL 更新到另一个域
http://domain2.example/otherfolder/img1.jpg
keeping the name of the file as is.保持文件名不变。
What's the query must I run?我必须运行什么查询?
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/%');
relevant docs: http://dev.mysql.com/doc/refman/5.5/en/string-functions.html#function_replace相关文档: http ://dev.mysql.com/doc/refman/5.5/en/string-functions.html#function_replace
Try using the REPLACE function :尝试使用替换功能:
mysql> SELECT REPLACE('www.example.com', 'w', 'Ww');
-> 'WwWwWw.example.com'
Note that it is case sensitive.请注意,它区分大小写。
Try this...尝试这个...
update [table_name] set [field_name] =
replace([field_name],'[string_to_find]','[string_to_replace]');
You need the WHERE clause to replace ONLY the records that complies with the condition in the WHERE clause (as opposed to all records).您需要WHERE子句仅替换符合 WHERE 子句中条件的记录(而不是所有记录)。 You use % sign to indicate partial string: IE您使用%符号表示部分字符串:IE
LIKE ('...//example.com/images/%');
means all records that BEGIN with "...//example.com/images/"
and have anything AFTER (that's the %
for...)表示所有以"...//example.com/images/"
并在之后有任何内容的记录(这是...的%
)
Another example:另一个例子:
LIKE ('%http://example.com/images/%')
which means all records that contains "http://example.com/images/"
这意味着包含"http://example.com/images/"
的所有记录
in any part of the string...在字符串的任何部分...
First, have to check首先,必须检查
SELECT * FROM `university` WHERE course_name LIKE '%&%'
Next, have to update接下来,必须更新
UPDATE university
SET course_name = REPLACE(course_name, '&', '&') WHERE id = 1
Results: Engineering & Technology => Engineering & Technology结果:工程与技术 =>工程与技术
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.