У меня есть следующий JSON format
в statistics
таблицы mysql, а столбец - это stats
{
"stats":{
"gender":{
"male":40, //40 is percentage
"female":50
},
"cities":[
{
"name":"Melbourne",
"country":"AU",
"percentage":20
},
{
"name":"London",
"country":"GB",
"percentage":10
},
{
"name":"Sydney",
"country":"AU",
"percentage":14
}
]
}
}
select * from statistics as a where a.stats->'$.stats.gender.male' < 41
It returns the above row since male percentage is 40.
Мне нужно получить данные о стране AU и проценте 20.
Любое предложение будет благодарным.
Один из вариантов - использовать функцию JSON_CONTAINS.
Тестовое задание:
SET @'country' := '"AU"',
@'percentage' := '20';
SELECT
'stats',
'a'.'stats' -> '$.stats.cities[*].country',
'a'.'stats' -> '$.stats.cities[*].percentage'
FROM
'statistics' 'a'
WHERE
JSON_CONTAINS('a'.'stats' -> '$.stats.cities[*].country', @'country') AND
JSON_CONTAINS('a'.'stats' -> '$.stats.cities[*].percentage', @'percentage');
См. Db-скрипт.