doumei1908 2017-12-02 21:53
浏览 407

mariadb中的JSON_SET,不要插入数据

In the MariaDB documentation it says that JSON_SET inserts or updates information, while JSON_INSERT just inserts, and JSON_REPLACE only substitutes.

I am using the code below, and I need the information to be inserted, if it does not exist, or updated, if it already exists.

The problem is that it is only updating, when it already exists, and it does not insert, if it does not exist.

MariaDB docs

UPDATE myTable SET config = JSON_SET( config, '$.person.name', 'zeca', '$.person.details.age', '87' ) WHERE id = 127

If "details" already exist in the "config" field of myTable, "age" will be registered. If "details" still do not exist, I want to register details.age, but it is not registering.

  • 写回答

2条回答 默认 最新

  • douning5041 2017-12-02 22:14
    关注

    I can´t test my code, but maybe you can get it to work with something like this

    UPDATE myTable SET config = JSON_MERGE(config, '{"person": {}'), config = JSON_SET( config, '$.person.name', 'zeca', '$.person.details.age', '87' ) WHERE id = 127

    评论

报告相同问题?

悬赏问题

  • ¥35 平滑拟合曲线该如何生成
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 自己瞎改改,结果现在又运行不了了
  • ¥15 链式存储应该如何解决
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站