Re: finding items with 0 rels for a 0 to many relationship

From: "Josh Tolley" <eggyknap(at)gmail(dot)com>
To: danmcb <danielmcbrearty(at)gmail(dot)com>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: finding items with 0 rels for a 0 to many relationship
Date: 2007-06-21 16:57:51
Message-ID: e7e0a2570706210957n4b511e0bqf7bb78a8f90147b1@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On 6/21/07, danmcb <danielmcbrearty(at)gmail(dot)com> wrote:
> Hi
>
> I have two tables, say A and B, that have a many-to-many
> relationship, implemented in the usual way with a join table A_B.
>
> How can I economically find all the rows in table A whose id's are not
> in A_B at all (i.e. they have zero instances of B associated)?

Use a left join. For instance, say there are a.id and b.id columns,
which are the primary keys in A and B respectively. Also say A_B
contains columns aid and bid which reference a.id and b.id
respectively.

> SELECT * FROM A LEFT JOIN A_B ON (A.ID = A_B.AID) WHERE A_B.BID IS NULL;

- Josh

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Michael Glaesemann 2007-06-21 17:13:38 Re: finding items with 0 rels for a 0 to many relationship
Previous Message Masaru Sugawara 2007-06-21 16:29:00 Re: Subquery problems