我将以下架构与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";
怎么没有返回结果?
答案 0 :(得分:2)
将JSON_CONTAINS
与我感兴趣的JSON数组值一起使用:
select * from people where JSON_CONTAINS (value, {"friends": [{"friendId": "123"}]});