SQL查询替换基于通配符的字符串 [英] SQL query to replace string based on wildcardt

查看:352
本文介绍了SQL查询替换基于通配符的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的WP数据库上运行这种类型的查询,以删除所有id ="more-"的跨度实例:

UPDATE wp_posts SET post_content = REPLACE (
post_content,
'<p><span id="more-35075"></span></p>',
'');

但是在我的示例中,"more-"后面的数字是一个变量.如何使用通配符之类的方式编写此查询:span id ="more-*.

谢谢

解决方案

在MySQL 8.0及更高版本中,您可以使用缺少相同内容中,可以完成一些复杂的字符串操作.这基于您的确认 ,则该子字符串在一个值中仅出现一次.

REPLACE() 不对通配符,模式,正则表达式等有任何支持.它仅将给定的 fixed 子字符串替换为另一个 fixed 子字符串,且字符串较大.

相反,我们可以尝试提取post_content的某些部分.我们将使用部分后面的子字符串.

现在,我们只需 Concat() 这些部分以获取所需的post_content.您可以在此处找到各种String函数的详细信息: https://dev.mysql.com/doc/refman/8.0/zh-CN/string-functions.html

我还添加了WHERE条件,以便我们仅选择与给定子字符串条件匹配的行.

UPDATE wp_posts 
SET post_content = 
CONCAT( 
       SUBSTRING_INDEX(post_content, 
                       '<p><span id="more-', 
                       1), 
       SUBSTRING(post_content, 
                 LOCATE('"></span></p>', 
                        post_content, 
                        LOCATE('<p><span id="more-',
                               post_content)
                        ) + 13) -- 13 is character length of "></span></p>
      )
WHERE post_content LIKE '%<p><span id="more-%"></span></p>%';


查询1:更新操作之前的数据

SELECT * FROM wp_posts;

| post_content                                            |
| ------------------------------------------------------- |
| adasdaadsa<p><span id="more-35075"></span></p>121324124 |
| 1412123123<p><span id="more-232"></span></p>adasdaafas  |


查询2:更新操作后的数据

SELECT * FROM wp_posts;

| post_content         |
| -------------------- |
| adasdaadsa121324124  |
| 1412123123adasdaafas |

在DB Fiddle上查看

I would like to run this type of query on my WP database to remove all span instances with id="more-":

UPDATE wp_posts SET post_content = REPLACE (
post_content,
'<p><span id="more-35075"></span></p>',
'');

But the number that follows the 'more-' in my example is a variable. How to write this query with someting like a wildcard: span id="more-*.

Thank you

解决方案

In MySQL version 8.0 and above, you can use REGEX_REPLACE() function. In absence of the same, some complicated string operations can be done. This is based on your confirmation, that the said sub-string is occurring only once in a value.

REPLACE() does not have any support for wildcards, patterns, regular expressions etc. It only replaces a given fixed substring with another fixed substring, in a bigger string.

Instead, we can try to extract portions of the post_content. We will extract the leading substring before the '<p><span id="more-' using Substring_Index() function. Similarly, we will extract the trailing substring after the '"></span></p>' portion.

Now, we can simply Concat() these portions to get the required post_content. You can find details of various String functions used here: https://dev.mysql.com/doc/refman/8.0/en/string-functions.html

I have also added a WHERE condition, so that we pick only those rows which match our given substring criteria.

UPDATE wp_posts 
SET post_content = 
CONCAT( 
       SUBSTRING_INDEX(post_content, 
                       '<p><span id="more-', 
                       1), 
       SUBSTRING(post_content, 
                 LOCATE('"></span></p>', 
                        post_content, 
                        LOCATE('<p><span id="more-',
                               post_content)
                        ) + 13) -- 13 is character length of "></span></p>
      )
WHERE post_content LIKE '%<p><span id="more-%"></span></p>%';


Query #1: Data before Update operations

SELECT * FROM wp_posts;

| post_content                                            |
| ------------------------------------------------------- |
| adasdaadsa<p><span id="more-35075"></span></p>121324124 |
| 1412123123<p><span id="more-232"></span></p>adasdaafas  |


Query #2: Data after Update operations

SELECT * FROM wp_posts;

| post_content         |
| -------------------- |
| adasdaadsa121324124  |
| 1412123123adasdaafas |

View on DB Fiddle

这篇关于SQL查询替换基于通配符的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆