Re: One to many query question

From: Eric Clark <eclark(at)zerohp(dot)com>
To: Dave Dribin <dave-ml(at)dribin(dot)org>
Cc: "pgsql-sql(at)postgresql(dot)org" <pgsql-sql(at)postgresql(dot)org>
Subject: Re: One to many query question
Date: 2003-07-30 20:11:35
Message-ID: 1059595896.12276.16.camel@eric
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

On Wed, 2003-07-30 at 12:35, Dave Dribin wrote:
> CREATE TABLE cd (
> id integer unique,
> artist varchar(25),
> title varchar(25)
> );
>
> CREATE TABLE cd_genres (
> cd_id integer,
> genre varchar(25)
> );

I think you've got this backwards. There is no advantage in the above
table's over simply having a genre varchar(25) in the cd table.

You really want:

CREATE TABLE genre (
genre_id serial,
genre varchar(25)
);

CREATE TABLE cd (
cd_id integer unique,
artist varchar(25),
title varchar(25),
genre_id varchar(25) references genre (genre_id)
);

> How do I write a query to find all CDs that are NOT Rock? A co-worker
> showed me the following query:

Now the query is simple:

SELECT cd.*, genre.genre FROM cd, genre WHERE cd.genre_id =
genre.genre_id AND genre.genre != 'Rock';

Hope that helps,
Eric

In response to

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message Dave Dribin 2003-07-30 20:27:36 Re: One to many query question
Previous Message Jamie Lawrence 2003-07-30 20:07:32 Fwd: Bad Join moment - how is this happening?