基于通配符替换字符串的 SQL 查询

2022-01-05 00:00:00 replace mariadb mysql wordpress phpmyadmin

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

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

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

谢谢

解决方案

在 MySQL 8.0 及以上版本,您可以使用 REGEX_REPLACE() 函数.在 相同的缺失中,可以完成一些复杂的字符串操作.这是基于您的确认,该子串在一个值中只出现一次.

REPLACE() 不支持通配符、模式、正则表达式等.它只在更大的字符串中用另一个 fixed 子字符串替换给定的 fixed 子字符串.

相反,我们可以尝试提取 post_content 的部分内容.我们将使用 '<p><span id="more-' 之前的前导子字符串/en/string-functions.html#function_substring-index" rel="nofollow noreferrer">Substring_Index() 函数.同样,我们将提取 ' 之后的尾随子串></span></p>' 部分.

现在,我们可以简单地Concat() 这些部分获得所需的post_content.您可以在此处找到使用的各种字符串函数的详细信息:https://dev.mysql.com/doc/refman/8.0/en/string-functions.html

我还添加了一个 WHERE 条件,以便我们只选择那些符合给定子字符串条件的行.

更新 wp_posts设置 post_content =康卡特(SUBSTRING_INDEX(帖子内容,'<p><span id="more-",1),SUBSTRING(帖子内容,LOCATE('"></span></p>',帖子内容,LOCATE('<p><span id="more-',post_content)) + 13) -- 13 是 "></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

相关文章