Re: Locating all Children given Set of Parents

From: Josh Berkus <josh(at)agliodbs(dot)com>
To: Adam Sherman <adam(at)tritus(dot)ca>, pgsql-sql(at)postgresql(dot)org
Subject: Re: Locating all Children given Set of Parents
Date: 2003-05-13 16:47:46
Message-ID: 200305130947.46844.josh@agliodbs.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: 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_child
where pc.child_id = children.id
and pc.parent_id IN ($parent_id_list)
);

--
Josh Berkus
Aglio Database Solutions
San Francisco

In response to

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message jwang 2003-05-13 17:18:52 insert problem with special characters
Previous Message Bruno Wolff III 2003-05-13 16:38:14 Re: N all-way relationship