我的字符串看起来像这样:
@N05/3835412983/academy-of-sciences.html
3835412983
和academy-of-sciences.html
在名为wp_4_posts
的列中名为post_content
的数据库表中的位置不同。
我是否可以通过通配符搜索来保留两个/
和条*.htm*
之间的唯一ID? @05
左侧的所有内容都无法更改。需要保留/
之间的数字。第二个/
之后的项目可以完全剥离。
手动完成,有数百个需要这样做。
对于上述情况,最终结果将是:
@N05/3835412983/
该结果将被写回数据库。
答案 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";