blunder
05-24-2007, 12:23 PM
I have two tables, articles and series. Not all articles are part of a serie, but some are. I want to have a query that displays all the articles, with latest on top, no matter if they are a serie or not, but here comes the problem.
From a series I only want to display the oldest article that is not rated. Some articles are rated and then they should no longer be displayed as part of that serie. Although the listing as a whole is date DESC I still want the selection of the one article matching the entry in the series table to be date ASC.
Is there any way to do what I want to do?
I thought about doing a UNION between two SELECT queries, one for only-articles and one for articles that are part of a serie. The problem is with the one for series. How do I do it?
What I thought I could do is, select all the entries from the series table, match it up with one distinct row from articles sorted by art_date ASC where art_ratings IS NULL. Then a UNION with a normal select from articles where art_serie IS NULL. Ordering both selects with art_date DESC somehow in the end.
Added creates and data below.
CREATE TABLE `series` (
`id` int( 11 ) NOT NULL AUTO_INCREMENT ,
`name` varchar( 140 ) NOT NULL ,
`parts` int( 11 ) NOT NULL ,
UNIQUE KEY `serieid` ( `id` )
)
CREATE TABLE `articles` (
`art_id` int( 11 ) NOT NULL AUTO_INCREMENT ,
`art_title` varchar( 150 ) NOT NULL ,
`art_date` datetime NOT NULL ,
`art_ratings` int( 11 ) default NULL ,
`art_serie` int( 11 ) default NULL ,
`art_seriepart` int( 11 ) default NULL ,
UNIQUE KEY `art_id` ( `art_id` )
)
INSERT INTO `series` ( `id` , `name` , `parts`)
VALUES ( '1', 'Serie One' , '4'),
( '2', 'Serie Two' , '2'),
( '3', 'Serie Three' , '2'),
( '4', 'Serie Four' , '2'),
( '5', 'Serie Five' , '2');
INSERT INTO `articles` ( `art_id` , `art_title` , `art_date` , `art_ratings` , `art_serie` , `art_seriepart` )
VALUES ( NULL , 'Part 1 of Serie 1', '2007-03-20 06:17:03', '1' , '1', '1'),
( NULL , 'Part 2 of Serie 1', '2007-04-01 07:17:03', '1' , '1', '2' ),
( NULL , 'Part 3 of Serie 1', '2007-04-03 07:17:03', NULL , '1', '3'),
( NULL , 'Part 4 of Serie 1', '2007-04-05 07:17:03', NULL , '1', '4'),
( NULL , 'Part 1 of Serie 2', '2007-05-01 07:17:03', '1' , '2', '1'),
( NULL , 'Part 2 of Serie 2', '2007-05-05 01:17:03', NULL , '2', '2' ),
( NULL , 'Part 1 of Serie 3', '2007-02-20 07:17:03', NULL , '3', '1'),
( NULL , 'Part 2 of Serie 3', '2007-03-12 07:17:03', NULL , '3', '2' ),
( NULL , 'Part 1 of Serie 4', '2007-01-10 07:17:03', '1' , '4', '1'),
( NULL , 'Part 2 of Serie 4', '2007-01-28 07:17:03', NULL , '4', '2' ),
( NULL , 'Part 1 of Serie 5', '2007-05-01 07:17:03', NULL , '5', '1'),
( NULL , 'Part 2 of Serie 5', '2007-05-02 07:17:03', '1' , '5', '2' ),
( NULL , 'Stand-alone Article 1', '2007-05-03 07:17:03', NULL , NULL, NULL),
( NULL , 'Stand-alone Article 2', '2007-03-29 07:17:03', '1' , NULL, NULL),
( NULL , 'Stand-alone Article 3', '2007-02-12 07:17:03', NULL , NULL, NULL),
( NULL , 'Stand-alone Article 4', '2007-03-31 07:17:03', NULL , NULL, NULL);
The data I want displayed:
'Part 2 of Serie 4', '2007-01-28 07:17:03', NULL , '4', '2'
'Stand-alone Article 3', '2007-02-12 07:17:03', NULL , NULL, NULL
'Part 1 of Serie 3', '2007-02-20 07:17:03', NULL , '3', '1'
'Stand-alone Article 4', '2007-03-31 07:17:03', NULL , NULL, NULL
'Part 3 of Serie 1', '2007-04-03 07:17:03', NULL , '1', '3'
'Part 1 of Serie 5', '2007-05-01 07:17:03', NULL , '5', '1'
'Stand-alone Article 1', '2007-05-03 07:17:03', NULL , NULL, NULL
'Part 2 of Serie 2', '2007-05-05 01:17:03', NULL , '2', '2'
From a series I only want to display the oldest article that is not rated. Some articles are rated and then they should no longer be displayed as part of that serie. Although the listing as a whole is date DESC I still want the selection of the one article matching the entry in the series table to be date ASC.
Is there any way to do what I want to do?
I thought about doing a UNION between two SELECT queries, one for only-articles and one for articles that are part of a serie. The problem is with the one for series. How do I do it?
What I thought I could do is, select all the entries from the series table, match it up with one distinct row from articles sorted by art_date ASC where art_ratings IS NULL. Then a UNION with a normal select from articles where art_serie IS NULL. Ordering both selects with art_date DESC somehow in the end.
Added creates and data below.
CREATE TABLE `series` (
`id` int( 11 ) NOT NULL AUTO_INCREMENT ,
`name` varchar( 140 ) NOT NULL ,
`parts` int( 11 ) NOT NULL ,
UNIQUE KEY `serieid` ( `id` )
)
CREATE TABLE `articles` (
`art_id` int( 11 ) NOT NULL AUTO_INCREMENT ,
`art_title` varchar( 150 ) NOT NULL ,
`art_date` datetime NOT NULL ,
`art_ratings` int( 11 ) default NULL ,
`art_serie` int( 11 ) default NULL ,
`art_seriepart` int( 11 ) default NULL ,
UNIQUE KEY `art_id` ( `art_id` )
)
INSERT INTO `series` ( `id` , `name` , `parts`)
VALUES ( '1', 'Serie One' , '4'),
( '2', 'Serie Two' , '2'),
( '3', 'Serie Three' , '2'),
( '4', 'Serie Four' , '2'),
( '5', 'Serie Five' , '2');
INSERT INTO `articles` ( `art_id` , `art_title` , `art_date` , `art_ratings` , `art_serie` , `art_seriepart` )
VALUES ( NULL , 'Part 1 of Serie 1', '2007-03-20 06:17:03', '1' , '1', '1'),
( NULL , 'Part 2 of Serie 1', '2007-04-01 07:17:03', '1' , '1', '2' ),
( NULL , 'Part 3 of Serie 1', '2007-04-03 07:17:03', NULL , '1', '3'),
( NULL , 'Part 4 of Serie 1', '2007-04-05 07:17:03', NULL , '1', '4'),
( NULL , 'Part 1 of Serie 2', '2007-05-01 07:17:03', '1' , '2', '1'),
( NULL , 'Part 2 of Serie 2', '2007-05-05 01:17:03', NULL , '2', '2' ),
( NULL , 'Part 1 of Serie 3', '2007-02-20 07:17:03', NULL , '3', '1'),
( NULL , 'Part 2 of Serie 3', '2007-03-12 07:17:03', NULL , '3', '2' ),
( NULL , 'Part 1 of Serie 4', '2007-01-10 07:17:03', '1' , '4', '1'),
( NULL , 'Part 2 of Serie 4', '2007-01-28 07:17:03', NULL , '4', '2' ),
( NULL , 'Part 1 of Serie 5', '2007-05-01 07:17:03', NULL , '5', '1'),
( NULL , 'Part 2 of Serie 5', '2007-05-02 07:17:03', '1' , '5', '2' ),
( NULL , 'Stand-alone Article 1', '2007-05-03 07:17:03', NULL , NULL, NULL),
( NULL , 'Stand-alone Article 2', '2007-03-29 07:17:03', '1' , NULL, NULL),
( NULL , 'Stand-alone Article 3', '2007-02-12 07:17:03', NULL , NULL, NULL),
( NULL , 'Stand-alone Article 4', '2007-03-31 07:17:03', NULL , NULL, NULL);
The data I want displayed:
'Part 2 of Serie 4', '2007-01-28 07:17:03', NULL , '4', '2'
'Stand-alone Article 3', '2007-02-12 07:17:03', NULL , NULL, NULL
'Part 1 of Serie 3', '2007-02-20 07:17:03', NULL , '3', '1'
'Stand-alone Article 4', '2007-03-31 07:17:03', NULL , NULL, NULL
'Part 3 of Serie 1', '2007-04-03 07:17:03', NULL , '1', '3'
'Part 1 of Serie 5', '2007-05-01 07:17:03', NULL , '5', '1'
'Stand-alone Article 1', '2007-05-03 07:17:03', NULL , NULL, NULL
'Part 2 of Serie 2', '2007-05-05 01:17:03', NULL , '2', '2'
