Skip to content
Advertisement

Select from many to many relations table

Given these tables:

scenarios-suites tables

I would like to query scenarios with all the connected suites.

results:

 **scenario**         **suites**
loginScenario         loginSignup, endToEnd, smoke
  addToCart           shoppingCart, paymentOptions, endToEnd

Please direct me to the preferred postgresql select query

Also the relation here is one (scenario) to many (suites) , if the design should be different please advise.

Advertisement

Answer

Use joins and string aggegation:

select 
    sc.description scenario_description, 
    string_agg(su.description, ', ') all_suites_descriptions
from scenario_suit scsu
inner join scenarios sc on sc.scenario_id = scsu.scenario
inner join suites su    on su.suite_id    = scsu.suite
group by sc.scenario_id

I made the assumption that table suites has a column called description, which you want to display.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement