dbTalk Databases Forums  

mysql query - grouping records

comp.databases comp.databases


Discuss mysql query - grouping records in the comp.databases forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Caitrin Torres
 
Posts: n/a

Default mysql query - grouping records - 08-09-2003 , 02:57 AM






I have a MySQL database that I use to organize a collection of fiction for
a website with PHP. I'm trying to provide a way to not display stories
that are part of long series, meaning that the displayed stories are
not part of any series or they are part of a series with fewer than six
books in it. The relevant tables in the database are set up like this:

CREATE TABLE seriesorder (
storyid int not null,
seriesid int not null,
position int not null,
primary key (storyid, seriesid)
)

CREATE TABLE story (
storyid int not null auto_increment,
title varchar(50),
// and other information, none of which concerns series
primary key storyid
)

Each story is associated with zero or more series. Each seriesid is
associated with at least two storyid's.

I need to end up with a temporary table that contains a list of storyids
that are not part of long series so that I can use this temporary table
in the query that does the work of displaying stories on the website.
I've come up with a workable solution (below), but the fact that it's in
three steps makes me suspect that there's a better way to do things. Is
there some way to combine these queries into something more efficient?

CREATE TEMPORARY TABLE longseries SELECT DISTINCT seriesid FROM
seriesorder GROUP BY seriesid HAVING count(*)>5;

CREATE TEMPORARY TABLE wrongids SELECT story.storyid FROM story,
seriesorder, longseries WHERE story.storyid=seriesorder.storyid AND
seriesorder.seriesid=longseries.seriesid;

CREATE TEMPORARY TABLE rightids SELECT story.storyid FROM story LEFT
JOIN wrongids ON story.storyid=wrongids.storyid WHERE wrongids.storyid
IS NULL;

Thanks for any help or advice,

-- Caitrin

Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.