2

我有以下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";没有返回结果呢?

4

1 回答 1

2

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

select * from people where JSON_CONTAINS (value, {"friends": [{"friendId": "123"}]});
于 2016-11-01T19:36:23.890 回答