簡體   English   中英

更新列值,替換字符串的一部分

[英]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

嘗試使用替換功能

mysql> SELECT REPLACE('www.example.com', 'w', 'Ww');
        -> 'WwWwWw.example.com'

請注意,它區分大小寫。

嘗試這個...

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 '%&amp%'

接下來,必須更新

UPDATE university
SET course_name = REPLACE(course_name, '&amp', '&') WHERE id = 1

結果:工程與技術 =>工程與技術

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM