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

编程入门 行业动态 更新时间:2024-10-26 13:34:45
本文介绍了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及更高版本中,您可以使用 REGEX_REPLACE() 函数.在缺少相同内容中,可以完成一些复杂的字符串操作.这基于您的确认 ,则该子字符串在一个值中仅出现一次.

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

相反,我们可以尝试提取post_content的某些部分.我们将使用 Substring_Index() 函数.同样,我们将提取'"></span></p>'部分后面的子字符串.

现在,我们只需 Concat() 这些部分以获取所需的post_content.您可以在此处找到各种String函数的详细信息: dev.mysql/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: dev.mysql/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查询替换基于通配符的字符串

本文发布于:2023-10-24 14:58:37,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1524245.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:通配符   字符串   SQL

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!