有没有一种快速的方法可以用 MySQL 更新一个 json 列的数据?
Is there a fast way to update one json column data with MySQL?
table中有一列为
table: articles
rows: id type: int(11)
json_data type: json
created_at type: datetime
updated_at type: datetime
现在 json_data
是
{
"version":"1",
"title":"A good product",
"body":"Very good",
"published_at":null
}
想要根据当前元数据将其数据更新为新的 json 模式格式:
{
"version":"2",
"items":[
{
"title":"A good product",
"body":"Very good",
"published_at":null
}
]
}
不会编程语言怎么办。 MySQL的流程可以吗?
UPDATE articles
SET json_data = JSON_OBJECT('version', 2,
'items', JSON_ARRAY(JSON_REMOVE(json_data, '$.version')));
If run the update command twice, the data will become nested and break the format - fiddle. Do you know how to avoid it even run many times? – iooi
添加适当的 WHERE,检查“$.version”,仅更新此值为 1 的那些行:
UPDATE articles
SET json_data = JSON_OBJECT('version', 2, 'items', JSON_ARRAY(JSON_REMOVE(json_data, '$.version')))
WHERE json_data->>"$.version" = 1;
table中有一列为
table: articles
rows: id type: int(11)
json_data type: json
created_at type: datetime
updated_at type: datetime
现在 json_data
是
{
"version":"1",
"title":"A good product",
"body":"Very good",
"published_at":null
}
想要根据当前元数据将其数据更新为新的 json 模式格式:
{
"version":"2",
"items":[
{
"title":"A good product",
"body":"Very good",
"published_at":null
}
]
}
不会编程语言怎么办。 MySQL的流程可以吗?
UPDATE articles
SET json_data = JSON_OBJECT('version', 2,
'items', JSON_ARRAY(JSON_REMOVE(json_data, '$.version')));
If run the update command twice, the data will become nested and break the format - fiddle. Do you know how to avoid it even run many times? – iooi
添加适当的 WHERE,检查“$.version”,仅更新此值为 1 的那些行:
UPDATE articles
SET json_data = JSON_OBJECT('version', 2, 'items', JSON_ARRAY(JSON_REMOVE(json_data, '$.version')))
WHERE json_data->>"$.version" = 1;