Search Postgresql Archives

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

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On 6/21/07, danmcb <danielmcbrearty@xxxxxxxxx> 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


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Index of Archives]     [Postgresql Jobs]     [Postgresql Admin]     [Postgresql Performance]     [Linux Clusters]     [PHP Home]     [PHP on Windows]     [Kernel Newbies]     [PHP Classes]     [PHP Books]     [PHP Databases]     [Postgresql & PHP]     [Yosemite]
  Powered by Linux