简体   繁体   English

如何按 id 删除 mySQL JSON 列元素,而不是按索引 num

[英]How to delete mySQL JSON column element by id, instead of by index num

Provided a datasource:提供数据源:

https://raw.githubusercontent.com/usnistgov/oscal-content/master/examples/ssp/json/ssp-example.json https://raw.githubusercontent.com/usnistgov/oscal-content/master/examples/ssp/json/ssp-example.json

I have added this to a JSON column called json_data in a table called ssp_models , with a record uuid of 66c2a1c8-5830-48bd-8fdd-55a1c3a52888 .我已将此添加到名为json_data的表中名为ssp_models的 JSON 列中,记录 uuid 为66c2a1c8-5830-48bd-8fdd-55a1c3a52888

记录在 myPHPAdmin 中的外观截图

I am able to GET records, INSERT records, and REMOVE records just fine from the "parties" node with that dataset.我能够从具有该数据集的“各方”节点中很好地获取记录、插入记录和删除记录。

However, it's the JSON_REMOVE part that's not working quite as I'd like.但是, JSON_REMOVE部分并没有像我想要的那样工作。 Presently I can json_remove an entry via indexnum via:目前我可以通过 indexnum json_remove 一个条目:

UPDATE ssp_models 
   SET json_data = JSON_REMOVE(json_data, '$.\"system-security-plan\".metadata.parties[0]') 

So that deletes entry [0] from 'parties' like you'd expect.这样就可以像您期望的那样从“各方”中删除条目 [0]。

But what I really want is to be able to delete a record by uuid.但我真正想要的是能够通过 uuid 删除记录。 As you can see in the 'parties' section of data, uuid is an element in each entry:正如您在数据的“各方”部分中看到的,uuid 是每个条目中的一个元素:

parties: [

{

uuid: "3b2a5599-cc37-403f-ae36-5708fa804b27",

type: "organization",

name: "Enterprise Asset Owners"

},

{

uuid: "833ac398-5c9a-4e6b-acba-2a9c11399da0",

type: "organization",

name: "Enterprise Asset Administrators"

}....

So how can I somehow combine 'WHERE uuid = "3b2a5599-cc37-403f-ae36-5708fa804b27"' for removal into such a sql query when it's part of the JSON Document Store syntax?那么,当它是 JSON 文档存储语法的一部分时,如何以某种方式将 'WHERE uuid = "3b2a5599-cc37-403f-ae36-5708fa804b27"' 组合到这样的 sql 查询中? (In that case, I'd expect it to remove the "Enterprise Asset Owners" entry along with it's type and uuid. It's like we need to add a search-function as part of the deletion, but the examples I've been reading don't provide that as an option within json_remove: https://database.guide/json_remove-remove-data-from-a-json-document-in-mysql/ (在这种情况下,我希望它会删除“企业资产所有者”条目以及它的类型和 uuid。就像我们需要添加搜索功能作为删除的一部分,但我一直在阅读的示例不要在 json_remove 中提供该选项: https://database.guide/json_remove-remove-data-from-a-json-document-in-mysql/

JSON_SEARCH() function might be used to find the element's index with the uuid value to be deleted such as JSON_SEARCH() function 可用于查找要删除的uuid值的元素索引,例如

UPDATE ssp_models 
   SET json_data = JSON_REMOVE(json_data, 
                       REPLACE(
                        JSON_UNQUOTE(
                            JSON_SEARCH(json_data, 
                                        'one', 
                                        '3b2a5599-cc37-403f-ae36-5708fa804b27')
                            ),'.uuid',
                       ''))

Demo 演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM