【问题标题】:MySQL 5.7.8 JSON merge new dataMySQL 5.7.8 JSON 合并新数据
【发布时间】:2016-07-11 07:02:21
【问题描述】:

我正在尝试使用新的 MySQL JSON 支持为管理区域创建一个注释 / cmets 系统。评论需要是可编辑的,我想在未来添加对其他东西的支持,也许是文件附件(将文件路径存储在 JSON 中,而不是文件本身!)。

    {
  "comments": [
    {
      "comment": "This is a comment",
      "user_id": 5,
      "datecreated": "2016-03-19"
    },
    {
      "comment": "This is a comment",
      "user_id": 1,
      "datecreated": "2016-03-19"
      "comments": [
        {
          "comment": "This is a sub-comment",
          "user_id": 4,
          "datecreated": "2016-03-19"
        },
        {
          "comment": "This is a sub-comment",
          "user_id": 4,
          "datecreated": "2016-03-19"
        }
      ]
    }
  ]
}

我认为有一种方法可以合并新数据,类似于 array_merge(),而无需每次都针对特定的键。

此查询有效,但它只针对一件事,即评论的文本内容。如果我想添加/编辑标签、图像或文件附件等,那么我需要一个很长的查询或多个查询。

UPDATE shared_notes SET json = JSON_REPLACE(json, "$.comments[1].comment", "This is a test comment") WHERE note_id = :note_id

我尝试将 JSON_REPLACE 和 JSON_SET 函数与 JSON_OBJECT 一起使用,但它会覆盖所有未指定的键,这意味着 user_id、datecreated 和任何子 cmets 都会被覆盖。

UPDATE shared_notes SET json = JSON_REPLACE(json, "$.comments[1]", JSON_OBJECT("comment", "This is a test comment") ) WHERE note_id = :note_id

这个 frankenstein 的查询几乎可以工作,但它实际上将更新后的评论连接到旧评论的末尾:

UPDATE shared_notes SET json = JSON_SET(json, "$.comments[1]", JSON_MERGE(JSON_EXTRACT(json, "$.comments[1]"), CAST('{"comment":"Test"}' AS JSON) ) ) WHERE note_id = :note_id

那么有没有更好的方法来使用 MySQL 轻松/动态更新 JSON 或仅针对 $.comments[1].comment$.comments[1][0].user_id 等?

【问题讨论】:

  • 天哪,我能感觉到你的痛苦。我不明白这样的函数怎么不是提供的 json 函数的一部分。这是一个基本的需求!

标签: mysql sql json mysql-json


【解决方案1】:

自 5.7.22 起,json_merge 已被弃用以供将来参考,取而代之的是 json_merge_preservejson_merge_patch

所以添加到@Adam Owczarczyk's 答案:

{...}
select json_merge_preserve('{"comments" : {"comment" : "This is a test comment" }}', comments)
from sampl_test;

【讨论】:

    【解决方案2】:

    这是一个很晚的答案,但仍然 - 你可以这样做:

    create table sampl_test(id int, comments json);
    insert into sampl_test values(1,
    '{
        "comments": [
            {
                "comment": "This is a comment",
                "user_id": 5,
                "datecreated": "2016-03-19"
            },
            {
                "comment": "This is a comment",
                "user_id": 1,
                "datecreated": "2016-03-19",
                "comments": [
                    {
                        "comment": "This is a sub-comment",
                        "user_id": 4,
                        "datecreated": "2016-03-19"
                    },
                    {
                        "comment": "This is a sub-comment",
                        "user_id": 4,
                        "datecreated": "2016-03-19"
                    }
                ]
            }
        ]
    }
    ')
    ;
    
    select json_merge('{"comments" : {"comment" : "This is a test comment" }}', comments)
    
    from sampl_test;
    

    【讨论】:

      猜你喜欢
      • 2019-02-28
      • 2016-01-20
      • 2020-06-30
      • 2014-11-14
      • 2022-01-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-23
      相关资源
      最近更新 更多