Re: Selecting records not present in related tables

From: Scott Marlowe <smarlowe(at)g2switchworks(dot)com>
To: Hector Rosas <jeziel(dot)rosas(at)gmail(dot)com>
Cc: pgsql-sql(at)postgresql(dot)org
Subject: Re: Selecting records not present in related tables
Date: 2005-10-06 20:13:41
Message-ID: 1128629621.29347.242.camel@state.g2switchworks.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

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

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message Frank Bax 2005-10-06 20:28:45 Re: Selecting records not present in related tables
Previous Message Anthony Molinaro 2005-10-06 20:11:44 Re: Selecting records not present in related tables