首页 > 解决方案 > MariaDB 10.2 / MySQL 5.7 使用通配符删除字段中的文本?

问题描述

我有一个描述字段,我想删除描述中匹配的某些文本的所有出现。请注意,81/29 将始终是不同的数字。有时它们可​​能是更多的数字,例如长度为 2-4。

样本数据:

To support this group, visit: https://example.com/give/81/29 The normal description continues here. There will be a lot of text below....

查询后:

The normal description continues here. There will be a lot of text below.... 

有没有办法用通配符搜索和替换?

例如:

UPDATE articles 
SET description = REPLACE(description, 'To support this group, visit: https://example.com/give/%/% ', '');

这显然不起作用。Replace 是否有任何通配符或 RegEX 功能?

标签: mysqlsqlreplacemariadbwildcard

解决方案


似乎您想从没有前导空格的最后一位数字开始提取摘录。所以使用:

UPDATE articles 
   SET description=LTRIM(
                         SUBSTRING( description, 
                                  - REGEXP_INSTR( REVERSE( description ) , '[0-9]' ) + 1 )
                   )  

在哪里确定

-->开始 SUBSTRING()

最后 --> REVERSE()

数字 --> REGEXP_INSTR()

没有前导空格 --> LTRIM()

使用函数。

Demo


推荐阅读