如何从数据库中删除自定义变量关键字?

时间:2019-12-18 16:33:48

标签: mysql sql

数据库包含类似于以下内容的条目;

[caption id="attachment_2792" align="aligncenter" width="600"]<img class="size-full wp-image-2792" src="https://www.blabla.com/wp-content/uploads/2018/09/Ultimate-Fishing-Simulator-5.jpg" alt="" width="600" height="337" /> Simple Keyword 1[/caption]

我想要的输出应该是这种方式(删除简单关键字1);

[caption id="attachment_2792" align="aligncenter" width="600"]<img class="size-full wp-image-2792" src="https://www.blabla.com/wp-content/uploads/2018/09/Ultimate-Fishing-Simulator-5.jpg" alt="" width="600" height="337" /> [/caption]

简而言之; 我想删除/> [/ caption]之间的所有内容。

2 个答案:

答案 0 :(得分:1)

您可以尝试使用

<div class="post-content-wrapper">
  <div class="flex flex7">
    <div class="child flex7-child flex7-child-1"></div>
    <div class="child flex7-child flex7-child-2"></div>
    <div class="child flex7-child flex7-child-3"></div>
    <div class="child flex7-child flex7-child-4"></div>
    <div class="child flex7-child flex7-child-5"></div>
  </div>
</div>

  select concat(left(your_col, locate('/>', your_col) -1)
 ,right(your_col, length(your_col) - locate('[/caption]',your_col)+1 ))
 from your_table;

您可以使用

来更新数据库
set @str ='[caption id="attachment_2792" align="aligncenter" width="600"]<img class="size-full wp-image-2792" src="https://www.blabla.com/wp-content/uploads/2018/09/Ultimate-Fishing-Simulator-5.jpg" alt="" width="600" height="337" /> Simple Keyword 1[/caption]';

select concat(left( @str, locate('/>',  @str) -1)
       ,right( @str, length( @str) - locate('[/caption]', @str)+1 ))

答案 1 :(得分:0)

这对我有用;

UPDATE 1y5D0_posts 
SET post_content = 
CONCAT( 
       SUBSTRING_INDEX(post_content, 
                       ' /> ', 
                       1), 
       SUBSTRING(post_content, 
                 LOCATE('[/caption]', 
                        post_content, 
                        LOCATE(' /> ',
                               post_content)
                        ) + 0)
      )
WHERE post_content LIKE '%/>%[/caption]%';