JSON_CONTAINS 无法从 MySQL 中找到

JSON_CONTAINS unable to find from MySQL

在 MySQL 中,我使用 PHP 的 json_encode:

将数据存储在“jsonValues”字段中
{"product_category":[[{"category":["28"]},{"product":["16","22","64"]}]]}

通过PHP,我想通过编写如下查询来获取数据:

SELECT * FROM `dbTable` 
WHERE JSON_CONTAINS(jsonValues, '"category":["28"]');

但是,它显示错误:

3141 - Invalid JSON text in argument 2 to function json_contains: "The document root must not follow by other values." at position 10.

您是否尝试过像 mysql 文档那样在搜索词周围添加大括号?

SELECT * FROM `dbTable` WHERE JSON_CONTAINS(jsonValues, '{"category":["28"]}');

是returns写完下面代码后的行:

SELECT * FROM `dbTable` WHERE JSON_CONTAINS(jsonValues, '{"category":["28"]}', '$.product_category');

如果上述方法的 None 适合您,请尝试下面的代码,它应该会起作用。

SELECT * FROMdbTable WHERE JSON_CONTAINS(jsonValues, '{"product_category": [[{"category": ["28"]}]]}');

我遇到了同样的问题, 我在数据库中保存数据如下,

$id = '1';
json_encode(array($id)); //in DB it stores like this => ["1"]

我改成了

$id = '1';
json_encode(array(intval($id))); //in DB it stores like this => [1]

那么这个查询就完美了。

Select * from table where WHERE JSON_CONTAINS(json_col, '$id');

希望对以后的人有所帮助。