简体   繁体   English

如何删除 MySQL 的 JSON 列中的键

[英]How to delete keys in JSON column of MySQL

My data is dirty, and now I am trying to correct them.我的数据很脏,现在我正在尝试纠正它们。

The data is like:数据是这样的:

mysql> select attrs from goods  limit 10;
+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| attrs                                                                                                                                                                                                                                                                    |
+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| {"logo": "1", "cover": "", "level": 0, "tag_6": 8, "summary": "L1", "showStatus": 0, "classAmount": 0, "productType": 0, "courseEndTime": 1532572800000, "courseStartTime": 1531706400000}  |
| {"logo": "1", "cover": "", "level": 0, "tag_6": 8, "summary": "L1", "showStatus": 0, "classAmount": 0, "productType": 0, "courseEndTime": 1533717600000, "courseStartTime": 1532851200000}  |
| {"logo": "1", "cover": "", "level": 0, "tag_6": 8, "summary": "L1", "showStatus": 0, "classAmount": 0, "productType": 0, "courseEndTime": 1534851600000, "courseStartTime": 1533985200000}  |
| {"logo": "1", "cover": "", "level": 0, "tag_6": 8, "summary": "L2", "showStatus": 0, "classAmount": 0, "productType": 0, "courseEndTime": 1532594400000, "courseStartTime": 1531728000000}  |
| {"logo": "1", "cover": "", "level": 0, "tag_147": 8, "tag_145": 2 "summary": "L2", "showStatus": 0, "classAmount": 0, "productType": 0, "courseEndTime": 1533728400000, "courseStartTime": 1532862000000}  |
| {"logo": "1", "cover": "", "level": 0, "tag_6": 8, "summary": "L2", "showStatus": 0, "classAmount": 0, "productType": 0, "courseEndTime": 1534819200000, "courseStartTime": 1533952800000}  |
| {"logo": "1", "cover": "", "level": 0, "tag_127": 8, "summary": "NGL", "showStatus": 0, "classAmount": 0, "productType": 0, "courseEndTime": 1532605200000, "courseStartTime": 1531738800000} |
| {"logo": "1", "cover": "", "level": 0, "tag_6": 8, "summary": "NGL", "showStatus": 0, "classAmount": 0, "productType": 0, "courseEndTime": 1533696000000, "courseStartTime": 1532829600000} |
|
+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+

What I want to remove some (key, value) pairs in the column attrs if any of these pairs exists.如果这些对中的任何一个存在(key, value)我想删除列attrs中的一些(key, value)对。 For example tag_147 and tag_124 are dirty, then any hit pair would be removed.例如tag_147tag_124是脏的,那么任何命中对都将被删除。

{"tag_147": 1, "tag_124": 2}  ===>  {}
{"tag_147": 1 "tag_100":2}  ==>  {"tag_100": 2}

How can i achieve it?我怎样才能实现它? Thanks.谢谢。

What I have tried is 1. find attrs that contains keys tag_147 and tag_124 .我尝试过的是 1. 找到包含键tag_147tag_124 attrs 2. Then update attrs. 2. 然后更新 attrs。 My original Sql is like:我原来的 Sql 是这样的:

update goods
set json_remove(*XXX*) 
where (select json_keys(attrs) from goods ) contains  (*tag_147*, *tag_127*)

Here I was blocked by constructing the contains statement...在这里,我被构造contains语句阻止了......

If you just want to delete those two tags from the attrs values that contain both of them, you can use JSON_REMOVE on any attrs values found by JSON_CONTAINS_PATH , using the all mode of JSON_CONTAINS_PATH to only find values which contain both tags:如果你只是想删除来自这两个标签attrs同时包含他们的价值观,你可以使用JSON_REMOVE任何attrs通过发现价值JSON_CONTAINS_PATH ,使用all的模式JSON_CONTAINS_PATH只找到其中包含这两个标签值:

UPDATE goods
SET attrs = JSON_REMOVE(attrs, '$.tag_147', '$.tag_124')
WHERE JSON_CONTAINS_PATH(attrs, 'all', '$.tag_147', '$.tag_124')

If you want to completely remove rows where the attrs values contains both tags, you can use the same JSON_CONTAINS_PATH condition in a DELETE query:如果要完全删除attrs值包含两个标签的行,可以在DELETE查询中使用相同的JSON_CONTAINS_PATH条件:

DELETE
FROM goods
WHERE JSON_CONTAINS_PATH(attrs, 'all', '$.tag_147', '$.tag_124')

Demo on dbfiddle dbfiddle 上的演示

If you want to delete those tags from the attrs values that contain any of them, you can remove the WHERE clause in the UPDATE ie如果您想从包含其中任何一个的attrs值中删除这些标签,您可以删除UPDATE中的WHERE子句,即

UPDATE goods
SET attrs = JSON_REMOVE(attrs, '$.tag_147', '$.tag_145')

And to delete rows with attrs values containing any of the tags, change the all parameter to JSON_CONTAINS_PATH to one :要删除attrs值包含任何标签的行,请将all参数更改为JSON_CONTAINS_PATHone

DELETE
FROM goods
WHERE JSON_CONTAINS_PATH(attrs, 'one', '$.tag_147', '$.tag_145')

Demo on dbfiddle dbfiddle 上的演示

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

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