X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (av.hub.org [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id 42CC7DA42C for ; Thu, 6 Oct 2005 17:13:43 -0300 (ADT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 43502-05 for ; Thu, 6 Oct 2005 20:13:40 +0000 (GMT) Received: from koolancexeon.g2switchworks.com (mail.g2switchworks.com [63.87.162.25]) by svr1.postgresql.org (Postfix) with ESMTP id D779EDA407 for ; Thu, 6 Oct 2005 17:13:40 -0300 (ADT) Received: mail.g2switchworks.com 10.10.1.8 from 10.10.1.37 10.10.1.37 via HTTP with MS-WebStorage 6.5.6944 Received: from state.g2switchworks.com by mail.g2switchworks.com; 06 Oct 2005 15:13:41 -0500 Subject: Re: Selecting records not present in related tables From: Scott Marlowe To: Hector Rosas Cc: pgsql-sql@postgresql.org In-Reply-To: <25266c5c0510061243g13c3f49cudde160c67f961bca@mail.gmail.com> References: <25266c5c0510061243g13c3f49cudde160c67f961bca@mail.gmail.com> Content-Type: text/plain Content-Transfer-Encoding: 7bit Message-Id: <1128629621.29347.242.camel@state.g2switchworks.com> Mime-Version: 1.0 X-Mailer: Ximian Evolution 1.4.6 (1.4.6-2) Date: Thu, 06 Oct 2005 15:13:41 -0500 X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.006 required=5 tests=[AWL=0.006] X-Spam-Level: X-Archive-Number: 200510/66 X-Sequence-Number: 22919 On Thu, 2005-10-06 at 14:43, Hector Rosas wrote: > Hello, I'm trying to select records in a table not present in a > related table, in example, I've a table with message information > (subject, message, date, etc) and another (usermessages) with where > user(s) has that message, its state, etc. Records in this table will > be deleted in a certain time (just some extra info). > I want to select messages records that aren't present in the other > table (usermessages), I got the next two queries, maybe someone can > suggest a better one. A fairly common way to do this is to use a left join and a not null: select a.id from tablea a left join tableb b on (a.id=b._aid) where b._aid IS NULL