如何在 MySQL 5.7 中更新嵌套的 JSON 数据类型 [英] How to update nested JSON data type in MySQL 5.7
问题描述
使用 JSON_SET 更新简单的 JSON 数据类型没有问题,但嵌套它不起作用.
Using JSON_SET I have no problem updating simple JSON datatype, but with nested it just doesn't work.
查询格式如下:
{
"textures":[
{
"label":"test",
"types":{
"t_1":0,
"t_2":0
}
},
{
"label":"KEK",
"types":{
"t_1":0,
"t_2":0
}
}
],
"weapons":[
{
"name":"WW_SHT",
"ammo":0
},
{
"name":"WW_DSS",
"ammo":0
}
]
}
有些行可能是空的,有些行不会有武器"结构.
Some rows might be empty, some rows won't have "weapons" structure.
我尝试了什么:
UPDATE `player` SET `info` = COALESCE(
JSON_MERGE(
`info`,
JSON_OBJECT('textures',
JSON_OBJECT('types',
JSON_OBJECT('t_1', '1', 't_2', '0')
)
)
),
JSON_INSERT(
JSON_OBJECT(),
'$.textures',
JSON_OBJECT('types',
JSON_OBJECT('t_1', '1', 't_2', '0')
)
)
);
我想更新 t_1 以将值从 0 更改为 1
I want to update t_1 to change value from 0 to 1
推荐答案
你的代码真的很复杂.尝试使用 json_set 或 json_replace 以及类似
You are really convoluding your code. Try your update with json_set or json_replace and something like
... WHERE doc->"$.textures[*].types.t_1" = 0
你的代码有点难以阅读和理解,其中嵌入了许多函数
Your code is a little hard to read and comprehend with some many functions embedded together
顺便说一句,json_merge 在 MySQL 8 中已弃用
BTW json_merge is deprecated in MySQL 8
你会想要阅读 https://elephantdolphin.blogspot.com/2018/09/json-paths-and-mysql-json-functions.html
这篇关于如何在 MySQL 5.7 中更新嵌套的 JSON 数据类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!