Re: Locating all Children given Set of Parents - Mailing list pgsql-sql

From Josh Berkus
Subject Re: Locating all Children given Set of Parents
Date
Msg-id 200305130947.46844.josh@agliodbs.com
Whole thread Raw
In response to Locating all Children given Set of Parents  (Adam Sherman <adam@tritus.ca>)
Responses Re: Locating all Children given Set of Parents  (Adam Sherman <adam@tritus.ca>)
Re: Locating all Children given Set of Parents  (Adam Sherman <adam@tritus.ca>)
List pgsql-sql
Adam,

> I have three tables: parents, children & parent_child_mm.
>
> (So Parents and Children are in a Many-to-Many relationship.)
>
> Given a set of Parents, say 1, 2 & 3, how do I retrieve all children in an
> efficient manner? (I want each child to appear only once.)

Easy:

Select children.*
from children where exists (select pc_id from parent_childwhere pc.child_id = children.idand pc.parent_id IN
($parent_id_list));

--
Josh Berkus
Aglio Database Solutions
San Francisco



pgsql-sql by date:

Previous
From: Bruno Wolff III
Date:
Subject: Re: N all-way relationship
Next
From: Adam Sherman
Date:
Subject: Re: Locating all Children given Set of Parents