views:

281

answers:

4

Hi all.

I have a database of articles, which are stored in categories. For my homepage, I want to grab an article from each category (I don't care which). However, some articles are crossposted to multiple categories, so they come up twice.

I have a table called tblReview with the article fields (reviewID, headline, reviewText) and a table called tblWebsiteContent that tells the site which categories the articles are in (id, reviewID, categoryID) and finally, a table called tblCategories (categoryID, categoryName) which stores the categories.

My query basically joins these tables and uses GROUP BY tblCategory.categoryID. If I try adding 'tblReview.reviewID' into the GROUP BY statement, I end up with hundreds of articles, rather than 22 (the number of categories I have).

I have a feeling this needs a subquery but my test efforts haven't worked (not sure which query needs to contain my joins / field list / where clause etc).

Thanks!

Matt

A: 

Try using SELECT DISTINCT. (This will only work if your SELECT is only pulling the article ID.)

chaos
You're right, this works, but unfortunately I need all the other fields (headline, reviewText, etc) and with 4 joins (removed the others from my post for simplicity's sake) in this query I don't want to run another one on the results... thanks though!
Matt Andrews
A: 

select DISTINCT reviewID

n00b32
A: 
SELECT T.categoryName, tR.headline, tR.reviewText
FROM (
    SELECT tC.categoryName, MAX(tR1.reviewID) reviewID
    FROM tblReview tR1 join tblWebsiteContent tWC on tR1.reviewID = tWC.reviewID
                      join tblCategory tC on tC.categoryID = tWC.categoryID
    GROUP BY tC.categoryName) T JOIN
    tblReview.tR on tR.reviewID = T.reviewID

this query will select for each category an article headline corresponding to the Max reviewId for that category (you said 'I don't care which')

najmeddine
I had to modify that query to get it to run:SELECT T.categoryName, tR.headline, tR.reviewTextFROM ( SELECT tC.categoryName, MAX(tR1.reviewID) reviewID FROM tblReview tR1 join tblWebsiteContent tWC on tR1.reviewID = tWC.reviewID join tblCategory tC on tC.categoryID = tWC.categoryID GROUP BY tC.categoryName) TJOIN tblReview tR on tR.reviewID = T.reviewIDIt still returns duplicate reviewID values though! :( Thank you anyway.
Matt Andrews
Sorry about the formatting, not sure how to format code on this site and can't find a help page for it... essentially I just changed the final line into a JOIN - is this right?
Matt Andrews
right! I fixed that.
najmeddine
you mean for each category you have more than 1 headline? reviewID is a primary key right?
najmeddine
Yeah, each category has lots of articles, but reviewID is the primary key.
Matt Andrews
Edit: do you mean my resultset? It returned a row for each category (correct), but the same headline appeared for some of the categories, where that article had been posted to both categories.
Matt Andrews
I see what do you want exactly. But what to do if 2 categories contain only 1 article and it's the same for both? It will be shown for the 1st, but the second will be 'empty' in your resultset?
najmeddine
Hmm good point, I didn't think of that. Maybe I should just do this server side instead (eg using PHP)?
Matt Andrews
what you want can not be done with SQL only. Use either PHP or a stored procedure.
najmeddine
Done, using PHP: http://pastebin.com/f1bdade8d (breaks my pagination functions a little since the queries expect to return a set number of rows, some of which are then deleted...) but still.
Matt Andrews
A: 

you can use the select distinct as a subselect, no?

noobsaibot