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 8E899D1C4EB for ; Wed, 30 Jul 2003 21:19:19 +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 89521-06 for ; Wed, 30 Jul 2003 18:18:18 -0300 (ADT) Received: from anchor-post-31.mail.demon.net (anchor-post-31.mail.demon.net [194.217.242.89]) by svr1.postgresql.org (Postfix) with ESMTP id 68F88D1C4C9 for ; Wed, 30 Jul 2003 18:19:17 -0300 (ADT) Received: from mwynhau.demon.co.uk ([193.237.186.96] helo=mainbox.archonet.com) by anchor-post-31.mail.demon.net with esmtp (Exim 3.35 #1) id 19hyLu-000CuM-0V; Wed, 30 Jul 2003 22:19:18 +0100 Received: from localhost (localhost.localdomain [127.0.0.1]) by mainbox.archonet.com (Postfix) with ESMTP id EF878167DD; Wed, 30 Jul 2003 22:19:16 +0100 (BST) Received: from client17.archonet.com (client17.archonet.com [192.168.1.17]) by mainbox.archonet.com (Postfix) with ESMTP id 450BA167D9; Wed, 30 Jul 2003 22:19:16 +0100 (BST) From: Richard Huxton To: Dave Dribin , pgsql-sql@postgresql.org Subject: Re: One to many query question Date: Wed, 30 Jul 2003 22:19:16 +0100 User-Agent: KMail/1.5 References: <20030730193517.GB1383@neo.realtors.org> In-Reply-To: <20030730193517.GB1383@neo.realtors.org> MIME-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: 7bit Content-Disposition: inline Message-Id: <200307302219.16508.dev@archonet.com> X-Virus-Scanned: by AMaViS snapshot-20020531 X-Virus-Scanned: by amavisd-new at postgresql.org X-Archive-Number: 200307/472 X-Sequence-Number: 14586 On Wednesday 30 July 2003 20:35, 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) > ); > > How do I write a query to find all CDs that are NOT Rock? A co-worker > showed me the following query: Basically you need to find all the CDs that ARE "rock" and subtract that set from the set of all CDs. You could use: SELECT id,artist,title FROM cd WHERE NOT EXIST (SELECT 1 FROM cd_genres WHERE cd_id=id AND genre='Rock'); or SELECT id,artist,title,cd_id FROM cd LEFT JOIN (SELECT cd_id FROM cd_genres WHERE genre='Rock') AS rock_cds ON cd_id=id WHERE cd_id IS NULL; or an EXCEPT clause etc. See which you like better. -- Richard Huxton Archonet Ltd