A
A
Anton2018-09-07 13:03:41
PostgreSQL
Anton, 2018-09-07 13:03:41

How to write sql query?

tell me how to write a SQL query (PostgreSQL)
there are 2 tables:
company(
1 name
2 events = many_to_many(event)
)
event (
1 name
)
1 linking table -
company_events (
1 id
2 company_id
3 event_id
)
how to write a sql query to output like this:
id |company name.| list of events separated by commas
id |company name.| list of events separated by commas
id |company name.| list of events separated by commas

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Decadal, 2018-09-07
@anttoon

select company.*, string_agg(events.name, ', ') from  company JOIN  company_events 
    ON company_events.company_id = company.id
JOIN  events
    ON events.id = company_events.event_id
GROUP BY company.id;

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question