php - 数据未从 mysql json 数据类型返回

标签 php mysql json jsonschema

我通过where条件从mysql的json字段搜索数据。但没有数据显示。

“attributes_list”的MySql字段名称,为json类型

我的 json 数据:

{
 "0": 
    {
      "id": "01c93aae-8603-42f3-b097-89357467872a#####ed20eb29-611a-4e5c-a966-c09708c98179", 
      "text": "Canada", 
      "attr_name": "Country"
    }, 
 "1": 
    {
      "id": "01c93aae-8603-42f3-b097-89357467872a#####6e6269a1-afbb-4d5d-b2ee-6d25fe38fec7", 
      "text": "Ontario", 
      "attr_name": "REGION"
    }, 
 "2": 
    {
      "id": "5a0a085e-f3e4-4fbe-9f3e-61cec4836d36#####d81923c9-9532-4375-ba3d-19214ac61c4e", 
      "text": "Lager", 
      "attr_name": "TYPE"
    }, 
 "-1": {"id": "", "text": "", "attr_name": ""}
}

我的mysql json查询:

当我运行以下查询时显示数据

SELECT * FROM products where attributes_list->'$[0]."0".text' = 'Canada';

但是当我运行这个时没有显示数据

SELECT * FROM products where attributes_list->'$[0]."*".text' = 'Canada'; 

我解释一下我的情况:

产品表中有 6 行。在产品表中,有一个字段名称为属性列表。

该字段为json类型。一个产品可以保存多个属性。我的 attribute_list json 格式是

{"0":{"id":"","text":""},"1":{"id":"","text":""}}. 

现在我想搜索 where text='Canada' 然后阅读这些产品信息

最佳答案

根据mySql manual选择器 $**.text 将被使用。

[在页面上搜索(路径$**.b)即可到达。]

这将搜索每个文档的第二级,并返回第二级文本键的值。 所以你的查询将是这样的:

SELECT * FROM products where attributes_list->'$.*.text' = 'Canada';

我也尝试执行与您上面提到的相同的任务 fiddle

关于php - 数据未从 mysql json 数据类型返回,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41457939/

相关文章:

mysql - 如何在 mysql workbench 中从 phpmyadmin 检索数据

mysql数据需要写入json文件

php - 与 SecureNet 支付网关集成

php - MySQL - 完整行检索与部分行检索

php - 如何扩大PHP变量的范围?

php - CakePHP 没有为 hasOne 关系拉动超过一定数量的行

php - SQL DELETE 最高值

json - 使用 JQuery Mobile 和 Json 动态注入(inject)页面

python - Pandas 数据框到 flask 模板作为 json

php - 由于不推荐使用 MSSQL,如何让 SQLSRV 扩展与 PHP 一起使用?