mariadb 10.2/mysql 5.7是否使用通配符删除字段中的文本?

axr492tv  于 2021-07-26  发布在  Java
关注(0)|答案(2)|浏览(319)

我有一个描述字段,我想删除描述中出现的所有匹配文本。请注意,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函数?

vaj7vani

vaj7vani1#

使用字符串函数 LOCATE() 以及 INSTR() :

UPDATE articles 
SET description = SUBSTRING(
  description, 
  LOCATE(' ', description, INSTR(description, 'https')) + 1
);

请看演示。

b0zn9rqh

b0zn9rqh2#

似乎您想从最后一位开始提取摘录,但不带前导空格。所以使用:

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

在哪里确定
从--> SUBSTRING() 最后--> REVERSE() 数字--> REGEXP_INSTR() 没有前导空格--> LTRIM() 使用函数。
演示

相关问题