X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [64.117.224.130]) by svr1.postgresql.org (Postfix) with ESMTP id 3C626D1C922 for ; Wed, 30 Jul 2003 21:12:48 +0000 (GMT) Received: from svr1.postgresql.org ([64.117.224.193]) by localhost (neptune.hub.org [64.117.224.130]) (amavisd-new, port 10024) with ESMTP id 89885-04 for ; Wed, 30 Jul 2003 18:11:47 -0300 (ADT) Received: from jalapeno.crazydogs.org (jalapeno.jellybean.co.uk [212.78.70.100]) by svr1.postgresql.org (Postfix) with ESMTP id 17EF6D1C92D for ; Wed, 30 Jul 2003 18:12:46 -0300 (ADT) Received: from rp by jalapeno.crazydogs.org with local (Exim 3.35 #1 (Debian)) id 19hyFb-0001Ci-00 for ; Wed, 30 Jul 2003 22:12:47 +0100 Date: Wed, 30 Jul 2003 22:12:47 +0100 From: Richard Poole To: pgsql-sql@postgresql.org Subject: Re: One to many query question Message-ID: <20030730211247.GA8892@guests.deus.net> References: <20030730193517.GB1383@neo.realtors.org> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20030730193517.GB1383@neo.realtors.org> User-Agent: Mutt/1.3.28i X-Virus-Scanned: by amavisd-new at postgresql.org X-Archive-Number: 200307/495 X-Sequence-Number: 14609 On Wed, Jul 30, 2003 at 02:35:20PM -0500, Dave Dribin wrote: > Hi, I'm having trouble with what I think should be an easy query. For > simplicity, I will use a CD database as an example. Each CD may have > multiple genres. Here's some sample data: > > Artist Title Genres > ---------------------- ------------------------------ ---------------- > Miles Davis Some Kind of Blue Jazz > Metallica Ride the Lightning Rock > Chemical Brothers Surrender Electronic > Radiohead OK Computer Rock, Electronic > > For simplicities sake, let's ignore normalization on artist and genre, > and say the tables look like: > > CREATE TABLE cd ( > id integer unique, > artist varchar(25), > title varchar(25) > ); > > CREATE TABLE cd_genres ( > cd_id integer, > genre varchar(25) > ); Assuming that each CD can have several rows in cd_genres, SELECT id, artist, title FROM cd WHERE id NOT IN ( SELECT cd_id FROM cd_genres WHERE genre = 'Rock' ); will do what you want. Your co-worker is perhaps used to certain lesser databases which don't support subselects... Richard