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 5C7A8D1C90E for ; Wed, 30 Jul 2003 21:26:52 +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 89906-06 for ; Wed, 30 Jul 2003 18:25:51 -0300 (ADT) Received: from exchange.hq.openratings.com (unknown [141.149.165.226]) by svr1.postgresql.org (Postfix) with ESMTP id 504EED1C4EC for ; Wed, 30 Jul 2003 18:26:50 -0300 (ADT) Received: from openratings.com (cancun.hq.openratings.com [10.0.0.225]) by exchange.hq.openratings.com with SMTP (Microsoft Exchange Internet Mail Service Version 5.5.2653.13) id PNZ5FD6K; Wed, 30 Jul 2003 17:26:52 -0400 Message-ID: <3F2837FF.7020805@openratings.com> Date: Wed, 30 Jul 2003 17:26:23 -0400 From: Dmitry Tkach User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.2.1) Gecko/20021130 X-Accept-Language: en-us, en MIME-Version: 1.0 To: Dave Dribin Cc: pgsql-sql@postgresql.org Subject: Re: One to many query question References: <20030730193517.GB1383@neo.realtors.org> In-Reply-To: <20030730193517.GB1383@neo.realtors.org> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Virus-Scanned: by amavisd-new at postgresql.org X-Archive-Number: 200307/473 X-Sequence-Number: 14587 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? > What about select * from cd where not exists (select 1 from cd_genres where cd_id = cd.id and genre='Rock')? Dima