MySQL 用通配符替换

MySQL for replace with wildcard(MySQL 用通配符替换)
本文介绍了MySQL 用通配符替换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写 SQL 更新以用新字符串替换特定的 xml 节点:

I'm trying to write a SQL update to replace a specific xml node with a new string:

UPDATE table
SET Configuration = REPLACE(Configuration,
     "<tag>%%ANY_VALUE%%</tag>"
     "<tag>NEW_DATA</tag>");

这样

SDADAS

变成

NEW_DATA

这种类型的请求是否缺少语法?

Is there a syntax im missing for this type of request?

推荐答案

更新:MySQL 8.0 有一个功能 REGEX_REPLACE().

Update: MySQL 8.0 has a function REGEX_REPLACE().

以下是我 2014 年的回答,它仍然适用于 8.0 之前的任何版本的 MySQL:

Below is my answer from 2014, which still applies to any version of MySQL before 8.0:

REPLACE() 不支持通配符、模式、正则表达式等.REPLACE() 仅将一个常量字符串替换为另一个常量字符串.

REPLACE() does not have any support for wildcards, patterns, regular expressions, etc. REPLACE() only replaces one constant string for another constant string.

你可以尝试一些复杂的事情,挑出字符串的前导部分和字符串的尾随部分:

You could try something complex, to pick out the leading part of the string and the trailing part of the string:

UPDATE table
SET Configuration = CONCAT(
      SUBSTR(Configuration, 1, LOCATE('<tag>', Configuration)+4),
      NEW_DATA,
      SUBSTR(Configuration, LOCATE('</tag>', Configuration)
    )

但这不适用于多次出现 的情况.

But this doesn't work for cases when you have multiple occurrences of <tag>.

您可能需要将行提取回应用程序,使用您喜欢的语言执行字符串替换,然后将行回传.换句话说,每行的三步过程.

You may have to fetch the row back into an application, perform string replacement using your favorite language, and post the row back. In other words, a three-step process for each row.

这篇关于MySQL 用通配符替换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Execute complex raw SQL query in EF6(在EF6中执行复杂的原始SQL查询)
Hibernate reactive No Vert.x context active in aws rds(AWS RDS中的休眠反应性非Vert.x上下文处于活动状态)
Bulk insert with mysql2 and NodeJs throws 500(使用mysql2和NodeJS的大容量插入抛出500)
Flask + PyMySQL giving error no attribute #39;settimeout#39;(FlASK+PyMySQL给出错误,没有属性#39;setTimeout#39;)
auto_increment column for a group of rows?(一组行的AUTO_INCREMENT列?)
Sort by ID DESC(按ID代码排序)