mysql - Mysql:如何替换字符串的一部分?
问题描述
我想删除以下网址中“x”之后的所有内容:
我有 :
url
/product/dtphdmi230rx?subtype=384
/product/dtphdmi230tx?subtype=385
/product/dtphdmi330rx?subtype=386
/product/dtphdmi330tx?subtype=387
我想要 :
url
/product/dtphdmi230rx
/product/dtphdmi230tx
/product/dtphdmi330rx
/product/dtphdmi330tx
我知道使用 mysql 8.0 很容易,regex_replace
但我无法更新我的服务器。mysql 5有什么办法吗?
注意:总是有一个“?” 在 urls 中,它可以是要删除的第一个字符。
感谢帮助
解决方案
只是:
left(url, locate('x?', url))
with mytable as (
select '/product/dtphdmi230rx?subtype=384' url
union all select '/product/dtphdmi230tx?subtype=385'
union all select '/product/dtphdmi330rx?subtype=386'
union all select '/product/dtphdmi330tx?subtype=387'
)
select left(url, locate('x?', url)) from mytable
| 左(网址,定位('x?',网址))| | :---------------------------- | | /产品/dtphdmi230rx | | /产品/dtphdmi230tx | | /产品/dtphdmi330rx | | /产品/dtphdmi330tx |
注意:正如 Raymond Nijland 所评论的,如果?
字符串中只出现一次并且可以用作要删除的第一个字符,您还可以执行以下操作:
left(url, locate('?', url) - 1)
推荐阅读
- django - ImportExportModelAdmin 给出错误 django.template.exceptions.TemplateDoesNotExist: admin/import_export/change_list_import_export.html
- qt - QML PageIndicator 适合矩形的大小
- python - 无法在系统上安装 PyAudio
- jquery - 在 bing 地图中为不同的邮政编码添加图钉
- flutter - 引发了另一个异常:错误:找不到正确的提供程序
在此 BlocSelector 上方 小部件 - c# - .NET 用于列表或单个项目的自定义 Json 转换器
- asp.net-mvc - 使用引导日期选择器对 MM/YYYY 进行 ASP NET MVC 客户端验证
- python - Tkinter .bind 无法绑定使用函数创建的按钮
- excel - 如何创建 COUNTIF 以查找包含长字符串的列的单元格中的文本
- github - 使用 GitHub NPM 注册表时出现 401 Unauthorized