【发布时间】:2020-10-08 10:51:31
【问题描述】:
我正在使用 MySQL 5.6.43 并尝试在表中的 JSON 数组中进行搜索。我在 MySQL 数据库中的 JSON 是这样的:
[
{
"Name": "AAA",
"CountryCode": "AFG",
"District": "Kabol",
"Population": 1780000
},
{
"Name": "BBB",
"CountryCode": "AFG",
"District": "Qandahar",
"Population": 237500
},
{
"Name": "CCC",
"CountryCode": "USD",
"District": "Qandahar",
"Population": 237500
}
]
我想拿刚刚AFGCountryCode的。所以我的结果应该是这样的:
[
{
"Name": "AAA",
"CountryCode": "AFG",
"District": "Kabol",
"Population": 1780000
},
{
"Name": "BBB",
"CountryCode": "AFG",
"District": "Qandahar",
"Population": 237500
}
]
我怎样才能做到这一点?
【问题讨论】:
-
解析对象数组以分离对象(使用 JSON_TABLE() 函数),检查每个对象的属性,然后聚合匹配的对象。
-
@Akina 感谢您的回复。能不能通过代码解释一下?