在数组中搜索JSON数据类型的对象属性时出现问题

时间:2016-09-08 00:18:13

标签: mysql jsonpath mysql-5.7 mysql-json

我将以下架构与value作为JSON类型

mysql> select * from people;
+------+---------------------------------------------------------------------------+
| id   | value                                                                     |
+------+---------------------------------------------------------------------------+
| blah | {"key1": "value1", "key2": "value2"}                                      |
| foo  | {"key1": "value1", "friends": [{"friendId": "123"}, {"friendId": "foo"}]} |
+------+---------------------------------------------------------------------------+

我希望下面的查询能够返回行foo,但它没有。

mysql> select * from people where value->'$.friends[*].friendId' = "123";
Empty set 

条件value->'$.friends[*].friendId'似乎有效,因为它适用于以下查询:

mysql> select value->'$.friends[*].friendId' from people;
+---------------------------------+
| value->'$.friends[*].friendId' |
+---------------------------------+
| NULL                            |
| ["123", "foo"]                  |
+---------------------------------+

那么查询select * from people where value->'$.friends[*].friendId' = "123";怎么没有返回结果?

1 个答案:

答案 0 :(得分:2)

JSON_CONTAINS与我感兴趣的JSON数组值一起使用:

select * from people where JSON_CONTAINS (value, {"friends": [{"friendId": "123"}]});