Received: from localhost (postgresql.org [64.49.215.8]) by postgresql.org (Postfix) with ESMTP id ADEB5475BEC for ; Tue, 26 Nov 2002 11:12:37 -0500 (EST) Received: from megazone.bigpanda.com (megazone.bigpanda.com [63.150.15.178]) by postgresql.org (Postfix) with ESMTP id D70C447650F for ; Tue, 26 Nov 2002 11:11:48 -0500 (EST) Received: by megazone.bigpanda.com (Postfix, from userid 1001) id 32C03D606; Tue, 26 Nov 2002 08:11:53 -0800 (PST) Received: from localhost (localhost [127.0.0.1]) by megazone.bigpanda.com (Postfix) with ESMTP id 2845E5C02; Tue, 26 Nov 2002 08:11:53 -0800 (PST) Date: Tue, 26 Nov 2002 08:11:53 -0800 (PST) From: Stephan Szabo To: Cc: Subject: Re: join question In-Reply-To: Message-ID: <20021126080514.C77510-100000@megazone23.bigpanda.com> MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII X-Virus-Scanned: by AMaViS new-20020517 X-Archive-Number: 200211/401 X-Sequence-Number: 10543 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;