在mySQL中搜索和替换特定模式

时间:2017-01-21 03:07:55

标签: mysql

我的字符串看起来像这样:

@N05/3835412983/academy-of-sciences.html

3835412983academy-of-sciences.html在名为wp_4_posts的列中名为post_content的数据库表中的位置不同。

我是否可以通过通配符搜索来保留两个/和条*.htm*之间的唯一ID? @05左侧的所有内容都无法更改。需要保留/之间的数字。第二个/之后的项目可以完全剥离。

手动完成,有数百个需要这样做。

对于上述情况,最终结果将是:

@N05/3835412983/

该结果将被写回数据库。

2 个答案:

答案 0 :(得分:0)

如果要提取中间元素,则可以使用substring_index()

select substring_index(substring_index(str, '/', 2), '/', -1)

这会提取第二个元素。

根据编辑,您似乎想要:

select concat(substring_index(str, '/', 2), '/')

如果您想更新该值,请:

update t
    set str = concat(substring_index(str, '/', 2), '/');

答案 1 :(得分:0)

更新特定字符串然后应使用replace()函数

语法: -

UPDATE table_name
SET column_name =
REPLACE(column_name,'OLD_STRING','NEW_STRING')
WHERE column_name = 'your condtition';

在你的情况下: -

UPDATE wp_4_posts
SET post_content =
REPLACE(post_content,'@N05/3835412983/academy-of-sciences.html','@N05/3835412983/');

如果您想要基于条件的结果

UPDATE wp_4_posts
SET post_content =
REPLACE(post_content,'@N05/3835412983/academy-of-sciences.html','@N05/3835412983/')
WHERE column_name = "values";
相关问题