简体   繁体   English

MySQL - 选择 json_column 具有特定值的行

[英]MySQL - Select rows where json_column has specific value

I have a MySQL table test with a column trip_info that contains JSON, for example:我有一个包含 JSON 的列trip_info的 MySQL 表测试,例如:

{
    "trips": [{
        "id": "261213_0",
        "type": "CD",
        "truck": "1847",
        "planbloc": "EE_001",
        "reuse": "EE_001",
        "status": "4",
        "stops": [{
            "208201": [{
                "handling": "S",
                "date": "2021-07-16"
            }],
            "D_517318_SZENTGOTTHARD": [{
                "handling": "S",
                "date": "2021-07-16"
            }],
            "T_857925_LOIPERSDORF - KITZLADEN": [{
                "handling": "C",
                "date": "2021-07-16"
            }],
            "T_895103_OBERWART": [{
                "handling": "C",
                "date": "2021-07-16"
            }],
            "D_613251_SCHACHENDORF": [{
                "handling": "S",
                "date": "2021-07-16"
            }],
            "T_894297_SZIGETSZENTMIKLOS": [{
                "handling": "D",
                "date": "2021-07-19"
            }]
        }]
    }]
}

I would like to select all rows in my table that contain a stop id '208201' with handling 'C'.我想选择我的表中包含处理 'C' 的停止 ID '208201' 的所有行。 I tried the following query without success:我尝试了以下查询但没有成功:

SELECT * FROM test WHERE JSON_CONTAINS(trip_info, '"208201"', '$.trips.stops') AND JSON_CONTAINS(trip_info, '"C"', '$.trips.stops.handling')

Can anyone help out?任何人都可以帮忙吗?

Try below queries:尝试以下查询:

MySQL v5.7 MySQL v5.7

SELECT * 
FROM test 
WHERE JSON_CONTAINS(JSON_UNQUOTE(trip_info->"$.trips[*].stops[*].""208201""[*].handling"), '"C"');

Check dbfiddle检查dbfiddle

MariaDB v10.3 MariaDB v10.3

SELECT * 
FROM test 
WHERE JSON_CONTAINS(JSON_UNQUOTE(JSON_EXTRACT(trip_info, "$.trips[*].stops[*].""208201""[*].handling")), '"C"');

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

相关问题 MySQL-仅选择列具有唯一值的行 - Mysql - Select only rows where column has unique value Select 行在 JSON 列中具有特定值 - Select rows with specific value in JSON column 如何 select 行,其中特定字符串列与 json 数组中的至少一个值匹配? - how to select rows where a specific string column matches at least one value inside a json array? Select 行,其中列具有具有相似 ID 的特定值 - Select rows where a column has specific values with similar ID's 如何选择数据库中特定列具有值的所有行? - How do you select all rows in a database where a specific column has a value? 具有原生JSON支持的MySQL 5.7-如何选择数组中存在特定值的行? - MySQL 5.7 with native JSON support - how to select rows where a specific value exists in array? 选择该列中特定列值为最大值的所有行 - Select all rows where specific column value is maximum of that column MySQL选择JSON数组列是另一个数组的子集的行 - MySQL select rows where the JSON array column is a subset of another array MySQL-选择列值仅为0的行,按另一列分组? - MySQL - Select rows where column value is only 0, group by another column? MySQL Select * FROM table WHERE a column's (JSON Object) specific value's string length is &lt;1 - MySQL Select * FROM table WHERE a column's (JSON Object) specific value's string length is <1
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM