【问题标题】:MariaDB/MySQL - How to query JSON based on key of nested objectMariaDB/MySQL - 如何根据嵌套对象的键查询 JSON
【发布时间】:2017-12-21 22:47:32
【问题描述】:

假设存储在 MariaDB/MySQL 中的以下 JSON 对象:

SET @j = '{
    "thing": {
        "sub_things": [
            {
                "attribute": [
                    { "1": 40 },
                    { "5": 25 },
                    { "13": 35 }
                ]
            },
            {
                "attribute": [
                    { "2": 50 },
                    { "7": 50 }
                ]
            }
        ]
    }
}'

我将如何查询它以返回 sub_things,其中 attribute 数组中的一个对象具有特定的键,例如其中键是 13 这应该返回第一个 sub_thing

谢谢!

【问题讨论】:

  • 虽然可能,但在JSON 中隐藏您想要搜索的内容并不一定是架构的最佳设计。

标签: mysql sql json mariadb


【解决方案1】:

我不确定在单个查询中是否可以实现所需。

下面的存储过程或许能给你一些想法(存储过程适用于 MariaDB 和 MySQL):

> DROP PROCEDURE IF EXISTS `JSON_query_based_on_key`;
Query OK, 0 rows affected (0.01 sec)

> DELIMITER //

> CREATE PROCEDURE `JSON_query_based_on_key`(
->   `json` TEXT,
->   `key` VARCHAR(5)
-> )
-> BEGIN
->   DECLARE `sub_things_current` INT
->      DEFAULT JSON_LENGTH(`json`, '$.thing.sub_things') - 1;
-> 
->   WHILE (`sub_things_current` > -1) DO
->     IF NOT JSON_CONTAINS_PATH(
->       `json`, 
->       'one', 
->       CONCAT('$.thing.sub_things[', `sub_things_current`, '].attribute[*]."', `key`, '"')
->     ) THEN
->       SET `json` := JSON_REMOVE(
->         `json`,
->         CONCAT('$.thing.sub_things[', `sub_things_current`, ']'));
->     END IF;
->     SET `sub_things_current` := `sub_things_current` - 1;
->   END WHILE;
-> 
->   SELECT JSON_EXTRACT(`json`, '$.thing');
-> END//
Query OK, 0 rows affected (0.00 sec)

> DELIMITER ;

> CALL `JSON_query_based_on_key`('{
'>   "thing": {
'>     "sub_things": [
'>       {
'>         "attribute": [
'>           { "1": 40 },
'>           { "5": 25 },
'>           { "13": 35 }
'>         ]
'>       },
'>       {
'>         "attribute": [
'>           { "2": 50 },
'>           { "7": 50 }
'>         ]
'>       }
'>     ]
'>   }
'> }', '13');
+---------------------------------------------------------------------+
| JSON_EXTRACT(`json`, '$.thing')                                     |
+---------------------------------------------------------------------+
| {"sub_things": [{"attribute": [{"1": 40}, {"5": 25}, {"13": 35}]}]} |
+---------------------------------------------------------------------+
1 row in set (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

根据需要修改代码。

请参阅 db<>fiddle 获取 MariaDB (10.2.6) 和 db-fiddle 获取 MySQL (5.7.17)。

【讨论】:

  • 很好的答案!!
猜你喜欢
  • 2020-09-30
  • 1970-01-01
  • 1970-01-01
  • 2013-03-18
  • 2016-05-05
  • 1970-01-01
  • 2023-02-25
  • 2013-04-06
相关资源
最近更新 更多