Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将动态文本从MySQL中的文本字符串移动到字符串中的另一个位置_Mysql - Fatal编程技术网

将动态文本从MySQL中的文本字符串移动到字符串中的另一个位置

将动态文本从MySQL中的文本字符串移动到字符串中的另一个位置,mysql,Mysql,我有几千条MySQL记录,其中包含保存在MySQL数据库中的文本字符串,这些字符串在URL变量“additional_development”后包含唯一值,每个记录中都包含类似“Chestnut%20Estates”的数据: <a class="button" href="http://www.websitedomain.com/search-results/?fullinfo=&Location=all&additional_development=Chestnut%20

我有几千条MySQL记录,其中包含保存在MySQL数据库中的文本字符串,这些字符串在URL变量“additional_development”后包含唯一值,每个记录中都包含类似“Chestnut%20Estates”的数据:

<a class="button" href="http://www.websitedomain.com/search-results/?fullinfo=&Location=all&additional_development=Chestnut%20Estates&additional_postal_code=
所以我尝试了不同程度的更新命令,但仍然没有效果

关于在MySQL查询中实现所需结果的最佳方法,有什么建议吗


表名是wp\u posts,列名是post\u content。

附加开发
唯一的动态部分吗?如果是,这是一个简单的字符串替换:

UPDATE table_name SET column_name=replace( column_name, '?fullinfo=&Location=all&additional_development=', '?fullinfo=' )
否则,您将不得不以另一种方式实现这一点,要么在MySQL中实现正则表达式替换您自己(这里有很多示例),要么通过select、语言中的regexp、update使用您选择的编程语言。当然,这样做比较慢,但您没有太多选择(除了切换数据库)


抱歉

请通过添加WHERE id
UPDATE t SET col = CONCAT(
    SUBSTRING_INDEX(col, '&Location', 1),
    SUBSTRING_INDEX(SUBSTRING_INDEX(col, '&additional_postal', 1), 'additional_development=', -1),
    REPLACE(
        SUBSTRING_INDEX(col, 'fullinfo=', -1),
        SUBSTRING_INDEX(SUBSTRING_INDEX(col, '&additional_postal', 1), 'additional_development=', -1),
        ''
    )
);

摘要

下面的SQL使用MySQL的字符串函数替换字符串的一部分。如果需要,它可以在一个查询中完成,而不是在两个查询中完成,但是SQL的可读性较差

演示

SQL

UPDATE wp_posts
SET post_content = 
  INSERT(post_content, 
         /* Replace from character after ?fullinfo= */
         LOCATE('?fullinfo=', post_content) + 10,
         /* Replace up to the next & */
         LOCATE('&', post_content, LOCATE('?fullinfo=', post_content) + 10) 
         - LOCATE('?fullinfo=', post_content) - 10,
         /* Replace with value of &additional_development parameter
            (i.e. substring after the = and before the next ampersand) */
         SUBSTRING(
           post_content,
           LOCATE('&additional_development=', post_content) + 24,
           LOCATE('&', post_content, LOCATE('&additional_development=', post_content) + 24)
           - LOCATE('&additional_development=', post_content) - 24));

UPDATE wp_posts
SET post_content = 
  INSERT(post_content,
         /* Replace from character after &additional_development= */
         LOCATE('&additional_development=', post_content) + 24, 
         /* Replace up to the next & */
         LOCATE('&', post_content, LOCATE('&additional_development=', post_content) + 24)
         - LOCATE('&additional_development=', post_content) - 24,
         /* Replace with empty string */
         '');

是的,但是在&additional_development=之后和&Location=之前的邻域名称值是动态的。我需要将其移动到after?fullinfo=我看不出您的更新实际上是如何将在这些位置找到的数据移动到新位置的?我的查询清除了
location=
query参数中的任何内容。从阅读你的问题,我不清楚,这一部分应该也移动。我以为你只是想把
附加开发
转移到
fullinfo
,我的简单替换就是这么做的(通过销毁位置部分)。对不起,这没用!只有该值位于URL-->中的位置,这是否意味着在所有URL中,您总是以相同的顺序使用相同的参数?我想用SUBSTR_索引可以做到这一点,但我需要先确认一下。它的顺序总是一样的,但不同的子分区的值是不同的。SUBSTR_索引是我想做的,然后我看到你先到了:)这是正确的纯SQL解决方案sure@Mihai-除非有多个,同一记录中字符串的类似实例。它会删除除最后一个实例之外的所有内容?我如何克服这个问题?请参阅示例:我认为这会起作用,但我需要包含/定位更多字符串,因为它正在进行我不希望的更改。我已经更新了我的原始问题,你能回顾一下并更新你的答案吗?当然-我已经相应地更新了我的答案(并修复了演示链接)。关闭,但仍然缺少这一部分吗?更新后的答案如何与这些方面相匹配:对不起,不太清楚你在这里问什么。演示中的结果与问题中要求的结果之间的唯一区别是后者缺少
/搜索结果
。因为你的q没有提到要删除这个,我认为这只是一个打字错误——你真的想删除这个吗?
UPDATE wp_posts
SET post_content = 
  INSERT(post_content, 
         /* Replace from character after ?fullinfo= */
         LOCATE('?fullinfo=', post_content) + 10,
         /* Replace up to the next & */
         LOCATE('&', post_content, LOCATE('?fullinfo=', post_content) + 10) 
         - LOCATE('?fullinfo=', post_content) - 10,
         /* Replace with value of &additional_development parameter
            (i.e. substring after the = and before the next ampersand) */
         SUBSTRING(
           post_content,
           LOCATE('&additional_development=', post_content) + 24,
           LOCATE('&', post_content, LOCATE('&additional_development=', post_content) + 24)
           - LOCATE('&additional_development=', post_content) - 24));

UPDATE wp_posts
SET post_content = 
  INSERT(post_content,
         /* Replace from character after &additional_development= */
         LOCATE('&additional_development=', post_content) + 24, 
         /* Replace up to the next & */
         LOCATE('&', post_content, LOCATE('&additional_development=', post_content) + 24)
         - LOCATE('&additional_development=', post_content) - 24,
         /* Replace with empty string */
         '');