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

标签 mysql jsonpath mysql-5.7 mysql-json

我有以下架构,为 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/

相关文章:

php - 不重复用户正确回答的问题的问题数据库

Json 路径 - 对列表列表进行过滤

mysql - 向 MBRContains LineFromText 中的函数 st_geometryfromtext 提供的 GIS 数据无效

MySql 查询不使用索引集

javascript - 使用 jsonpath 提取 json 中的元素值

MYSQL django测试错误: django. db.utils.InternalError : (7,“重命名时出错

mysql - 创建表 : You have an error in your SQL syntax near 'order( order_id INT UNSIGNED NOT NULL AUTO_INCREMENT, user_id ' at line 1 时出错

php - Laravel Eloquent 一对多

php - Mysql子查询多结果

JSONPath表达式从条件数组中获取值或仅获取第一个值