How to get the length of an array of objects in MySQL Json column where the containing objects met a condition

时间:2018-07-24 10:05:41

标签: mysql json

I am trying to query a Mysql json column that contains an array of objects. I want the length of the array where a condition is met in the objects

Example of the JSON array:

[
    {"userId": 100, "type": 1, "status": 1},
    {"userId": 101, "type": 2, "status": 1},
]

Current Query:

SELECT IFNULL(JSON_LENGTH(json_users), 0) AS my_count, post_id 
FROM posts WHERE post_id = 6421028173277829027 
AND json_contains(json_users, '{"type" : 1, "status": 1}');

This will return the following whic is desired

+------------+---------------------+
| my_count   | post_id             |
+------------+---------------------+
|          1 | 6421028173277829027 |
+------------+---------------------+

But if I change the query where param 'type' to say 3

SELECT IFNULL(JSON_LENGTH(json_users), 0) AS my_count, post_id 
FROM posts WHERE post_id = 6421028173277829027 
AND json_contains(json_users, '{"type" : 3, "status": 1}');

I get an empty set. But I'm trying to get the following:

+------------+---------------------+
| my_count   | post_id             |
+------------+---------------------+
|          0 | 6421028173277829027 |
+------------+---------------------+

I assume I have to change the where clause and somehow check inside the following:

IFNULL(JSON_LENGTH(## json_users ##), 0)

but I don't know where to start

EDIT:

The following query returns an array of paths to the objects so I can use JSON_LENGTH to get the count:

SELECT JSON_LENGTH(IFNULL(JSON_SEARCH(json_users->>'$[*].type', 'all', 1), JSON_ARRAY())) AS my_count
FROM posts WHERE post_id = 6421028173277829027;

1 个答案:

答案 0 :(得分:0)

您尝试过

SELECT JSON_LENGTH(JSON_EXTRACT(json_users, "$.type")) FROM posts

我知道存在一个错误,如果json_users为null,它将不会返回任何内容。

相关问题