你好朋友 我需要帮助来解决以下问题,
我在postgres db表中有一组记录,其中表具有JSONB
类型的字段。
JSONB
类型列包含以下JSON
,
记录#1:-
{
"key1": "value1",
"key2": "value2",
"audience": [
{
"name": "Person1",
"email": "test1@mail.com",
"country": "UK",
"primaryNumber": "+1234567890",
"secondaryNumber": "+1234567890"
},
{
"name": "Person2",
"email": "test2@mail.com",
"country": "UK",
"primaryNumber": "+1234567890",
"secondaryNumber": "+1234567890"
}
]
}
记录#2:-
{
"key1": "value1",
"key2": "value2",
"audience": [
{
"name": "Person3",
"email": "test3@mail.com",
"country": "UK",
"primaryNumber": "+1234567890",
"secondaryNumber": "+1234567890"
},
{
"name": "Person4",
"email": "test4@mail.com",
"country": "UK",
"primaryNumber": "+1234567890",
"secondaryNumber": "+1234567890"
}
]
}
预期结果(吸引所有受众):-
[
{
"name": "Person1",
"email": "test1@mail.com",
"country": "UK",
"primaryNumber": "+1234567890",
"secondaryNumber": "+1234567890"
},
{
"name": "Person2",
"email": "test2@mail.com",
"country": "UK",
"primaryNumber": "+1234567890",
"secondaryNumber": "+1234567890"
},
{
"name": "Person3",
"email": "test3@mail.com",
"country": "UK",
"primaryNumber": "+1234567890",
"secondaryNumber": "+1234567890"
},
{
"name": "Person4",
"email": "test4@mail.com",
"country": "UK",
"primaryNumber": "+1234567890",
"secondaryNumber": "+1234567890"
}
]
任何人都可以{strong>帮助我设计查询吗?native query
还是通过spring-data-jpa
?
如果有人能帮助我解决这种情况,我将非常感谢!
答案 0 :(得分:2)
您应该使用'audience'
提取每一行的jsonb_array_elements()
个数组元素,并使用jsonb_agg()
将它们聚合到单个json对象中:
select jsonb_agg(value)
from my_table
cross join jsonb_array_elements(json_data->'audience')