我有這些表SQL查詢來JSON結果
destination (id integer, name text)
activity (id integer, name text, destination_id integer)
activity_duration (value numeric, unit text, label text, activity_id integer)
activity_duration_price(amount numeric, currency_id integer, activity_duration_id integer)
每個目的地有1個或多個活動,每個活動具有1周或更大的持續時間和持續時間的每個具有1個價。
這裏是我的查詢
select
d.id destination_id,
d.name destination_name,
count(a.id) as nb_activity,
string_agg('{"id":'||a.id||',"name":"'||a.name||'","pictures":"'||a.pictures||'","meta":{"price":'||amount||',"label":"'||label||'"}}',',') activities
from destination d
left join activity a on (a.destination_id = d.id)
left join activity_duration ad on (activity_id = a.id)
left join activity_duration_price adp on (adp.activity_duration_id = ad.id)
where d.id = any (array[142]) group by d.id, d.name order by nb_activity desc
,這是爲string_agg
[{
"id": 2,
"name": "visit tokyo",
"meta": {
"price": 210,
"label": "Adult"
}
}, {
"id": 2,
"name": "visit tokyo",
"pictures": "{}",
"meta": {
"price": 170,
"label": "Children"
}
}]
結果正如你所看到的,元信息是有兩次相同的項目。
我想我對string_agg結果是這樣
[{
"id": 2,
"name": "visit tokyo",
"meta": [{
"price": 210,
"label": "Adult"
},
{
"price": 170,
"label": "Children"
}
]
}]
我怎樣才能得到這樣的結果?
您在查詢中有類似'amount'和'label'的字段,您在表中沒有提到這個字段。 – Dmitry
謝謝!我編輯了我的文章 – Rony