MySQL 函数更新 URL 列值的域

问题描述 投票:0回答:2

就说这张桌子吧

+--------------+---------------------------------+
| id           | path                            |
+--------------+---------------------------------+
| 1            | http://old.com/img/1.png        | 
| 2            | http://old.com/img/2.png        | 
| 3            | http://old.com/img/3.png        | 
| 4            | http://old.com/img/4.png        | 
| 5            | http://old.com/img/5.png        | 
| 6            | http://old.com/img/6.png        | 
+--------------+---------------------------------+

哪句话会更新为

+--------------+---------------------------------+
| id           | path                            |
+--------------+---------------------------------+
| 1            | http://new.com/img/1.png        | 
| 2            | http://new.com/img/2.png        | 
| 3            | http://new.com/img/3.png        | 
| 4            | http://new.com/img/4.png        | 
| 5            | http://new.com/img/5.png        | 
| 6            | http://new.com/img/6.png        | 
+--------------+---------------------------------+

我不知道如何替换 MySQL 中的字符串。

"UPDATE table SET path = REPLACE(path, 'old.com', 'new.com')"
??

我不能冒搞乱数据库的风险!

sql mysql replace
2个回答
1
投票
update mytable set path = replace(path, 'old.com', 'new.com');

如果您担心更新的影响(您应该担心),我首先这样做:

select path, replace(path, 'old.com', 'new.com') as new_path
from mytable;

我只是观察结果,看看它们是否“看起来不错”。如果他们这样做,请启动更新。


0
投票

mySQL 本身没有正则表达式替换功能:正则表达式函数仅匹配。这是 mysql 文档

© www.soinside.com 2019 - 2024. All rights reserved.