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 56030DA17D for ; Thu, 6 Oct 2005 17:28:59 -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 68567-07 for ; Thu, 6 Oct 2005 20:28:55 +0000 (GMT) Received: from tomts16-srv.bellnexxia.net (tomts16-srv.bellnexxia.net [209.226.175.4]) by svr1.postgresql.org (Postfix) with ESMTP id D470FD9743 for ; Thu, 6 Oct 2005 17:28:53 -0300 (ADT) Received: from acer.sympatico.ca ([65.95.139.218]) by tomts16-srv.bellnexxia.net (InterMail vM.5.01.06.10 201-253-122-130-110-20040306) with ESMTP id <20051006202854.QQBV21026.tomts16-srv.bellnexxia.net@acer.sympatico.ca> for ; Thu, 6 Oct 2005 16:28:54 -0400 Message-Id: <5.2.1.1.0.20051006162629.03400300@pop6.sympatico.ca> X-Sender: b1wudu74@pop6.sympatico.ca X-Mailer: QUALCOMM Windows Eudora Version 5.2.1 Date: Thu, 06 Oct 2005 16:28:45 -0400 To: pgsql-sql@postgresql.org From: Frank Bax Subject: Re: Selecting records not present in related tables In-Reply-To: <25266c5c0510061243g13c3f49cudde160c67f961bca@mail.gmail.co m> Mime-Version: 1.0 Content-Type: text/plain; charset="us-ascii"; format=flowed X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.092 required=5 tests=[AWL=0.042, FORGED_RCVD_HELO=0.05] X-Spam-Level: X-Archive-Number: 200510/65 X-Sequence-Number: 22918 At 03:43 PM 10/6/05, 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. > >SELECT m.id FROM messages AS m >WHERE (SELECT count(um.*) FROM usermessages AS um WHERE um.idmessage=m.id )=0; > >SELECT m.id FROM messages AS m where id NOT IN (select >um.idmessage FROM usermessages um); select m.id from messages as m left join usermessages as um on m.id=um.idmessage where um.idmessage is null;