由于某些原因,在我的数据库中,在表列中,我有一些带有绝对 URL 的数据,如下所示:
+---------------------------------+
| url |
+---------------------------------+
| /foo/12 |
| http://www.myexample.com/foo/13 |
| http://www.myexample.com/foo/14 |
+---------------------------------+
我想将绝对网址更新为相对网址,如下所示:
+---------------------------------+
| url |
+---------------------------------+
| /foo/12 |
| /foo/13 |
| /foo/14 |
+---------------------------------+
如何使用 SQL 查询执行此操作?
最佳答案
您可以使用REPLACE(str,from_str,to_str)
返回字符串 str,并将所有出现的字符串 from_str 替换为字符串 to_str。 REPLACE() 在搜索 from_str 时执行区分大小写的匹配。
因此您可以在更新查询中编写replace(url,' http://www.myexample.com ','')
update table_name set url = replace(url,'http://www.myexample.com','') where 1=1
关于mysql - 如何使用 SQL 根据模式更新列内的特定数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50005494/