在标签之后的“”之间搜索并替换字符串

时间:2014-01-22 05:48:48

标签: mysql search replace

我正在尝试更新我的mySql数据库。 Value的值是

{
    "Subject": "Subject here",
    "Content": "The content here",
    "TextContent": "Here text content goes",
    "To": "this is to user<to@example.com>",
    "From": "Name of User<from@example.com>"
}

现在该做什么我要搜索From代码,然后Replace Name of User<from@example.com>搜索other user<other@example.com>

From标记之后的值对我来说是未知的。所以我想在""引号之间替换所有内容。

对于我目前正在做的事情

UPDATE `Table_Name`
SET Value= REPLACE(Value,'Name of User<from@example.com>','other user<other@example.com>')
WHERE Name = 'Name_of_field' && INSTR(Value,'From')>0;

我知道这是错误的做法。请建议我一个方法,因为我在这里使用的Name of User<from@example.com>对我来说是未知的。

1 个答案:

答案 0 :(得分:2)

这应该这样做

UPDATE table1
   SET value = REPLACE(value, 
                       SUBSTRING_INDEX(SUBSTRING_INDEX(value, '"From": "', -1), '"', 1),
                       'other user<other@example.com>')
 WHERE ...

这是 SQLFiddle 演示