X-Original-To: pgsql-sql-postgresql.org@postgresql.org Received: from localhost (mx1.hub.org [200.46.208.251]) by postgresql.org (Postfix) with ESMTP id 6AB289FB2FA for ; Tue, 11 Jul 2006 13:00:59 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.208.251]) (amavisd-new, port 10024) with ESMTP id 78736-04 for ; Tue, 11 Jul 2006 13:00:50 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey- Received: from exchange.g2switchworks.com (mail.g2switchworks.com [63.87.162.25]) by postgresql.org (Postfix) with ESMTP id C947D9FB2F7 for ; Tue, 11 Jul 2006 13:00:48 -0300 (ADT) Received: from 10.10.1.37 ([10.10.1.37]) by exchange.g2switchworks.com ([10.10.1.2]) with Microsoft Exchange Server HTTP-DAV ; Tue, 11 Jul 2006 16:00:46 +0000 Received: from state.g2switchworks.com by mail.g2switchworks.com; 11 Jul 2006 11:00:46 -0500 Subject: Re: How to find entries missing in 2nd table? From: Scott Marlowe To: pgsql-sql@postgresql.org In-Reply-To: <20060711141951.GA17936@s2.yuriev.com> References: <20060711141951.GA17936@s2.yuriev.com> Content-Type: text/plain Content-Transfer-Encoding: 7bit Message-Id: <1152633646.6540.68.camel@state.g2switchworks.com> Mime-Version: 1.0 X-Mailer: Ximian Evolution 1.4.6 (1.4.6-2) Date: Tue, 11 Jul 2006 11:00:46 -0500 X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200607/181 X-Sequence-Number: 25755 On Tue, 2006-07-11 at 09:19, alex-lists-pgsql@yuriev.com wrote: > Hi, > I realize I probably lost my marbles but I've been having a god > awful time with a single query: > > control: > .... > .... > controller_id pk; > > > datapack: > > controller_id fk; > .... > .... > .... > > I need to get all entries from the table control that are not listed in > datapack. OK, this is a pretty common problem. Easy solution is left join / null. select t1.id from table1 t1 left join table table2 t2 on (t1.id=t2.id) where t2.id is null since a left join gives us all rows from the left table, and nulls where the right table doesn't match up, and we're asking for all the rows where t2.id is null, we'll get all the rows in t1 that don't have a match in t2. cool, huh?