mysql - 如何根据 SQL 模式更新列内的特定数据?
问题描述
由于某些原因,在我的数据库中,在表列中,我有一些带有绝对 url 的数据,如下所示:
+---------------------------------+
| url |
+---------------------------------+
| /foo/12 |
| http://www.myexample.com/foo/13 |
| http://www.myexample.com/foo/14 |
+---------------------------------+
我想像这样将绝对 url 更新为相对 url:
+---------------------------------+
| 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
推荐阅读
- javascript - 在 ASP.NET MVC 中使用 ADAL.JS
- javascript - 使用 crypto.subtle.digest("SHA-256", buffer) 散列大文件
- android - 读取来自特定端口的短信
- python - 如何在python中进行api调用?
- c# - 如何为 wpf 字符串绑定上标特定字符?
- powershell - Powershell用竖线替换引号
- javascript - 使用 iCheck-helper 时如何通过类获取下一个跨度元素值?
- security - 使用 OAuthBearer 的 Kafka SASL 身份验证机制?
- mobile - Flutter - Sliver List 内的 Sliver Layout 水平滚动
- sql-server - 如果 CTE 溢出到 tempDB,临时表会是更好的选择吗?