Re: One to many query question

From: Richard Poole <richard(at)ruthie(dot)org>
To: pgsql-sql(at)postgresql(dot)org
Subject: Re: One to many query question
Date: 2003-07-30 21:12:47
Message-ID: 20030730211247.GA8892@guests.deus.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

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

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message Richard Huxton 2003-07-30 21:19:16 Re: One to many query question
Previous Message Denis Zaitsev 2003-07-30 21:02:52 Re: Nonexistent NEW relation in some places of rules