[英]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:尝试以下查询:
SELECT *
FROM test
WHERE JSON_CONTAINS(JSON_UNQUOTE(trip_info->"$.trips[*].stops[*].""208201""[*].handling"), '"C"');
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.