gpt4 book ai didi

mysql - 更新 JSON 对象 MySQL

转载 作者:行者123 更新时间:2023-11-29 02:11:16 25 4
gpt4 key购买 nike

我想在 MySQL 中更新一个 JSON 对象。

表格

id (int-11, not_null, auto_inc)
labels (json)

JSON美化

[
{
"tagname": "FOO",
"category": "CAT_1",
"isnew": "no",
"isdeleted": "no"
},
{
"tagname": "BAR",
"category": "CAT_2",
"isnew": "yes",
"isdeleted": "no"
}
]

我想在现有对象旁边添加一个新的 TAG 元素 (JSON OBJECT),但不需要先选择字段并用文本更新所有字段。我有很多谷歌,但我还不能理解 MySQL 的 JSON 处理。我刚刚学会了如何像这样插入数据:

INSERT INTO `table_name`(
`id` ,
`labels`
)
VALUES(
null ,
JSON_ARRAY
(
JSON_OBJECT
(
"tagname", "FOO",
"category", "CAT_1",
"isnew", "no",
"isdeleted", "no"
),
JSON_OBJECT
(
"tagname", "BAR",
"category", "CAT_2",
"isnew", "yes",
"isdeleted", "no"
)

)
);

最佳答案

你可以使用 JSON_ARRAY_APPEND :

UPDATE tab
SET labels = JSON_ARRAY_APPEND(labels, '$',
JSON_OBJECT
(
"tagname", "BARX",
"category", "CAT_3",
"isnew", "yes",
"isdeleted", "no"
)
)
WHERE ID = 1;

DB-Fiddle.com demo

关于mysql - 更新 JSON 对象 MySQL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51845151/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com