如何在JSON列中的mysql中的现有JSON内插入JSON?

时间:2019-01-25 05:41:53

标签: mysql database

我想通过数据库查询将JSON数据插入MySQL中现有的JSON数据中。另外,如果JSON字段已经存在,则它不应执行任何操作。 例如,我的jsonTable分别具有id和jsonConfig列,而JSON字段包含某些id的数据,如下所示。

{
  "name": "Baljinder",
  "siblings": {
      "name": "Sukhjinder",
      "age": 29,
      "alive": true
   }
}

现在我要在兄弟姐妹的对象内插入下面给出的地址JSON对象。

"address": {
      "city": "Melbourne",
      "country": "Australia"
 }

这样我的最终结果就是这样。

{
  "name": "Baljinder",
  "siblings": {
      "name": "Sukhjinder",
      "age": 29,
      "alive": true,
      "address": {
          "city": "Melbourne",
          "country": "Australia"
      }
  }
}

1 个答案:

答案 0 :(得分:0)

我尝试过,这对我来说适用于MySQL 5.7.13

UPDATE jsonTable SET jsonConfig=JSON_INSERT(jsonConfig, "$.siblings.address", JSON_OBJECT('city','Melbourne','country','Australia')) where id = <?>;