我有以下架构,值
为 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";
没有返回结果呢?
最佳答案
将 JSON_CONTAINS
与我感兴趣的 JSON 数组值结合使用:
select * from people where JSON_CONTAINS (value, {"friends": [{"friendId": "123"}]});
关于mysql - 在数组中搜索 JSON 数据类型的对象属性时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39380787/