Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/58.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/73.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
JSON_CONTAINS()和MySQL中的JSON对象数组_Mysql_Sql_Json_Mysql Json - Fatal编程技术网

JSON_CONTAINS()和MySQL中的JSON对象数组

JSON_CONTAINS()和MySQL中的JSON对象数组,mysql,sql,json,mysql-json,Mysql,Sql,Json,Mysql Json,这是一个示例数据库“test”,其JSON列“arr”包含一个JSON对象数组 +----+----------------------------------------------------------+ | id | arr | +----+----------------------------------------------------------+ | 1 | [{&qu

这是一个示例数据库“test”,其JSON列“arr”包含一个JSON对象数组

+----+----------------------------------------------------------+
| id | arr                                                      |
+----+----------------------------------------------------------+
|  1 | [{"name": "aman"}, {"name": "jay"}]                      |
|  2 | [{"name": "yash"}, {"name": "aman"}, {"name": "jay"}] |
+----+----------------------------------------------------------+
我想使用JSON_CONTAINS来了解数组中对象的特定键中是否存在值

我的问题是:

SELECT JSON_CONTAINS(arr, '"jay"', '$[*].name') from test WHERE id=1;
我得到以下错误:

错误3149(42000):在这种情况下,路径表达式可能不包含*和**标记或数组范围

我知道我可以尝试使用
JSON\u EXTRACT()
来实现这一点,但我在这里做错了什么


有没有办法在MySQL中使用JSON对象数组的
JSON\u CONTAINS

是的,可以使用以下语法:

SELECT JSON_CONTAINS(arr, '{"name": "jay"}') from test WHERE id=1;

例如:

+-----+--------------------------------------------------------+---+
| id  |                          arr                           | r |
+-----+--------------------------------------------------------+---+
|  1  | [{"name": "aman"}, {"name": "jay"}]                    | 1 |
|  2  | [{"name": "yash"}, {"name": "aman"}, {"name": "jay"}]  | 1 |
|  3  | [{"name": "yash"}, {"name": "aman"}]                   | 0 |
+-----+--------------------------------------------------------+---+

您必须使用JSON_搜索:

SELECT JSON_SEARCH(arr, 'one', 'jay', NULL, '$[*].name') IS NOT NULL
FROM test 
WHERE id=1;