Re: join question

From: Stephan Szabo <sszabo(at)megazone23(dot)bigpanda(dot)com>
To: <nathan(at)ncyoung(dot)com>
Cc: <pgsql-sql(at)postgresql(dot)org>
Subject: Re: join question
Date: 2002-11-26 16:11:53
Message-ID: 20021126080514.C77510-100000@megazone23.bigpanda.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql


On Fri, 22 Nov 2002, Nathan Young wrote:

> Hi all.
>
> I have a table with members and a table with payments. Each payment is
> related to a member by memberID and each payment has (among other things) a
> year paid.
>
> I can create a join to find a list of members who have paid for a given year
> (2002 in this case):
>
> select member.memberID,member.name from member, payment where
> payment.memberID = member.memberID and payment.yearPaid = 2002
>
> I would like to be able to get a list of members who have not paid for a
> given year.

Well, I believe either of these two will do that:

select member.memberId, member.name from member where not exists (select
* from payment where payment.memberId=member.memberID and
payment.yearPaid=2002);

select member.memberId, member.name from member left outer join
(select * from payment where yearPaid=2002) as a using (memberId) where
yearPaid is null;

> I would also like to combine the two criteria, for example to generate a list
> of members who have paid for 2002 but not 2003.

I think these would do that:

select member.memberID,member.name from member, payment where
payment.memberID = member.memberID and payment.yearPaid = 1999
and not exists (select * from payment where
payment.memberId=member.memberId and yearPaid=2002);

select member.memberId, member.name from member inner join (select
* from payment where yearPaid=2002) as a using (memberId) left outer join
(select * from payment where yearPaid=2003) as b using (memberId) where
b.yearPaid is null;

In response to

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message dev 2002-11-26 17:04:33 Re: Are sub-select error suppressed?
Previous Message Charles H. Woloszynski 2002-11-26 16:05:27 Re: How does postgres handle non literal string values