pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Eric Clark <eclark@zerohp.com>
To: Dave Dribin <dave-ml@dribin.org>
Cc: pgsql-sql@postgresql.org <pgsql-sql@postgresql.org>
Subject: Re: One to many query question
Date: 30 Jul 2003 13:11:35 -0700
Message-ID: <1059595896.12276.16.camel@eric> (raw)
In-Reply-To: <20030730193517.GB1383@neo.realtors.org>
References: <20030730193517.GB1383@neo.realtors.org>

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




view thread (10+ messages)  latest in thread

Message-ID: <1059595896.12276.16.camel@eric>
Permalink:  ../1059595896.12276.16.camel@eric/
Also on:    postgresql.org/message-id/1059595896.12276.16.camel@eric

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: eclark@zerohp.com, dave-ml@dribin.org
  Subject: Re: One to many query question
  In-Reply-To: <1059595896.12276.16.camel@eric>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox