简体   繁体   English

如何在MySQL中搜索嵌套的JSON

[英]How to search nested JSON in MySQL

I am using MySQL 5.7+ with the native JSON data type.我正在使用 MySQL 5.7+ 和本机 JSON 数据类型。 Sample data:样本数据:

[
  {
    "code": 2,
    "stores": [
      {
        "code": 100,
        "quantity": 2
      },
      {
        "code": 200,
        "quantity": 3
      }
    ]
  },
  {
    "code": 4,
    "stores": [
      {
        "code": 300,
        "quantity": 4
      },
      {
        "code": 400,
        "quantity": 5
      }
    ]
  }
]

Question: how do I extract an array where code = 4 ?问题:如何提取code = 4的数组?

The following (working) query has the position of the data I want to extract and the search criterion hardcoded:以下(工作)查询具有我要提取的数据的 position 和硬编码的搜索条件:

SELECT JSON_EXTRACT(data_column, '$[0]') 
FROM   json_data_table
WHERE  data_column->'$[1].code' = 4

I tried using a wildcard ( data_column->'$[*].code' = 4 ) but I get no results in return.我尝试使用通配符 ( data_column->'$[*].code' = 4 ) 但我没有得到任何结果。

SELECT row FROM 
(
    SELECT data_column->"[*]" as row
    FROM   json_data_table
    WHERE  4 IN JSON_EXTRACT(data_column, '$[*].code')
) 
WHERE row->".code" = 4

... though this would be much easier to work with if this wasn't an unindexed array of objects at the top level. ...虽然如果这不是顶级的未索引对象数组,这将更容易使用。 You may want to consider some adjustments to the schema.您可能需要考虑对架构进行一些调整。

Note:注意:

If you have multiple rows in your data, specifying "$[i]" will pick that row, not the aggregate of it.如果您的数据中有多行,指定"$[i]"将选择该行,而不是它的聚合。 With your dataset, "$[1].code" will always evaluate to the value of code in that single row.对于您的数据集, "$[1].code"将始终评估为该单行中code的值。

Essentially, you were saying:基本上,你是在说:

  1. $ json collection $ json 集合
  2. [1] second object in the collection. [1]集合中的第二个对象。
  3. .code attribute labeled "code". .code属性标记为“代码”。

... since there will only ever be one match for that query, it will always eval to 4... ...因为该查询只会有一个匹配项,所以它总是 eval 为 4 ...

  1. WHERE 4 = 4

Alternate data structure if possible如果可能,替代数据结构

Since the entire purpose of "code" is as a key, make it the key.既然“代码”的全部目的是作为一把钥匙,那就让它成为钥匙。

[
  "code2":{
    "stores": [
      {
        "code": 100,
        "quantity": 2
      },
      {
        "code": 200,
        "quantity": 3
      }
    ]
  },
  "code4": {
    "stores": [
      {
        "code": 300,
        "quantity": 4
      },
      {
        "code": 400,
        "quantity": 5
      }
    ]
  }
]

Then, all it would require would be:那么,它所需要的只是:

SELECT datacolumn->"[code4]" as code4
FROM json_data_table

if you want to fetch nested json in mysql,valid json must exist in your column.如果要获取 mysql 中的嵌套 json,则列中必须存在有效​​的 json。 then you can try out那么你可以试试

JSON_EXTRACT(json_column,'$.level1.$level1.2') from table;

Get the full example获取完整示例

access full code 访问完整代码

This is what you are looking for.这就是你要找的。

SELECT data_column->'$[*]' FROM json_data_table where data_column->'$[*].code' like '%4%' . SELECT data_column->'$[*]' FROM json_data_table where data_column->'$[*].code' like '%4%'

The selected data will have [] around it when selecting from an array thus data_column->'$[*].code' = 4 is not possible.从数组中选择时,所选数据将有[]周围,​​因此data_column->'$[*].code' = 4是不可能的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM