Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/77.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 更新列值,替换字符串的一部分_Mysql_Sql - Fatal编程技术网

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

Mysql 更新列值,替换字符串的一部分,mysql,sql,Mysql,Sql,我在MySQL数据库中有一个包含以下列的表 [id, url] URL如下所示: http://domain1.com/images/img1.jpg 我想将所有URL更新到另一个域 http://domain2.com/otherfolder/img1.jpg 保持文件的名称不变 我必须运行什么查询 UPDATE urls SET url = REPLACE(url, 'domain1.com/images/', 'domain2.com/otherfolder/') 相关文档:尝

我在MySQL数据库中有一个包含以下列的表

[id, url]
URL如下所示:

 http://domain1.com/images/img1.jpg
我想将所有URL更新到另一个域

 http://domain2.com/otherfolder/img1.jpg
保持文件的名称不变

我必须运行什么查询

UPDATE urls
SET url = REPLACE(url, 'domain1.com/images/', 'domain2.com/otherfolder/')
相关文档:

尝试使用:


请注意,它区分大小写。

您需要使用WHERE子句来替换仅符合WHERE子句中条件的记录(与所有记录相反)。使用%符号表示部分字符串:即

LIKE ('...//domain1.com/images/%');
是指所有
“../domain1.com/images/”
开头且其后有任何内容的记录(这是…)的
%

另一个例子:

LIKE ('%http://domain1.com/images/%')
这意味着包含
的所有记录http://domain1.com/images/“

在字符串的任何部分…

尝试此

update [table_name] set [field_name] = 
replace([field_name],'[string_to_find]','[string_to_replace]');

首先,必须检查

大学
中选择*课程名称,如“%&%”

下一步,必须更新

更新大学 设置课程名称=REPLACE(课程名称,,,,,,,,,,,其中id=1


结果:Engineering&;Technology=>Engineering&;Technology

您好-为什么我需要where?@GuyCohen,因为否则查询将修改表中的每一行。
WHERE
子句优化查询以仅修改具有特定URL的行。从逻辑上讲,结果是相同的,但是添加
WHERE
将使操作更快。
WHERE
还确保只替换以
http://etc/etc/
要替换的字符串。
例如,在给定的答案中,
http://domain1.com/images/this/is/a/test
将受到影响,但
foobar/http://domain1.com/images/
不会。@UniversalGrass在这里看到了很多可能的答案:从未想过这是可能的!谢谢注意外壳(问我怎么知道的!):)这节省了几个小时,谢谢
LIKE ('%http://domain1.com/images/%')
update [table_name] set [field_name] = 
replace([field_name],'[string_to_find]','[string_to_replace]');