Answer the question
In order to leave comments, you need to log in
How to group data in postgresql?
I have two tables (one for portfolios, one for portfolio photos), how do I output an array of portfolios with their associated photos? How to build a query for SELECT?
[{
name: '', descripton: '',
...
images: [{src: 'url', src_build: 'url'}, {src: 'url', src_build: 'url'}]
}]
Answer the question
In order to leave comments, you need to log in
select
portfolio.id, portfolio.name,
array_agg(json_build_object('src', photos.src, 'src_build', photos.src_build))
from portfolio
left join photos on portfolio.id = photos.portfolio_id
group by portfolio.id, portfolio.name
;
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question