kwichz
kwichz

Reputation: 2453

Problem when grouping

I have this MySql query :

SELECT forum_categories.title, forum_messages.author, forum_messages.date AS last_message
FROM forum_categories
JOIN forum_topics ON forum_topics.category_id=forum_categories.id 
JOIN forum_messages ON forum_messages.topic_id=forum_topics.id
WHERE forum_categories.id=6
ORDER BY forum_categories.date ASC

And the output is the follow :

Welcome     daniel      2010-07-09 22:14:49
Welcome     daniel      2010-06-29 22:14:49
Welcome     luke        2010-08-10 20:12:20
Welcome     skywalker   2010-08-19 22:12:20
Welcome     delicious   2010-10-09 19:12:20
Welcome     daniel      2011-11-05 23:12:20
Welcome     pierre      2011-11-05 23:12:22

Now, I'd like to group it using the MAX date. So the query become :

SELECT forum_categories.title, forum_messages.author, forum_messages.date AS last_message, MAX(forum_messages.date)
FROM forum_categories
JOIN forum_topics ON forum_topics.category_id=forum_categories.id 
JOIN forum_messages ON forum_messages.topic_id=forum_topics.id
WHERE forum_categories.id=6
GROUP BY forum_categories.id
ORDER BY forum_categories.date ASC

PERFECT! The date taken is correct: 2011-11-05 23:12:22 (the MAX date for that grouping). But I also aspect from this behaviour to get the corrispondent user for that line (in the example pierre).

But it take another one.

Why? It doesnt select the whole row? And how can I fix this trouble? Cheers

EDIT

In fact, I should apply this grouping to this whole query :

SELECT forum_categories.title, COUNT(DISTINCT forum_topics.id) AS total_topics, SUM(CASE WHEN forum_messages.original=0 THEN 1 ELSE 0 END) AS total_replies, forum_messages.author, MAX(forum_messages.date) AS last_message
FROM forum_categories
JOIN forum_topics ON forum_topics.category_id=forum_categories.id 
JOIN forum_messages ON forum_messages.topic_id=forum_topics.id
GROUP BY forum_categories.id 
ORDER BY forum_categories.date

Upvotes: 1

Views: 116

Answers (2)

Galz
Galz

Reputation: 6842

Try this one (let me know if it works):

SELECT forum_categories.title, forum_messages.author, 
   forum_messages.date AS last_message
FROM forum_categories
JOIN forum_topics ON forum_topics.category_id=forum_categories.id
JOIN forum_messages ON forum_messages.topic_id=forum_topics.id
JOIN (SELECT MAX(m.date) as date, top.category_id  
     FROM forum_messages m
     JOIN forum_topics top ON m.topic_id = top.id
     GROUP BY top.category_id) as t
  ON t.category_id = forum_topics.category_id AND t.date = forum_messages.date
WHERE forum_categories.id=6
GROUP BY forum_categories.id

Here is a second option:

SELECT forum_categories.title, forum_messages.author, 
       forum_messages.date AS last_message
FROM forum_categories
JOIN forum_topics ON forum_topics.category_id=forum_categories.id
JOIN forum_messages ON forum_messages.topic_id=forum_topics.id
WHERE forum_categories.id=6
AND forum_messages.date = (SELECT MAX(date)
                           FROM forum_messages t
                           WHERE t.topic_id = forum_topics.id)
GROUP BY forum_categories.id
ORDER BY forum_categories.date ASC

Upvotes: 3

Alanyst
Alanyst

Reputation: 1410

You need to include forum_categories.title and forum_messages.author in your GROUP BY clause, since they are not aggregate functions. Think of GROUP BY as saying which values in the result set should be "smushed" together into one row.

Also you'll most likely want to order by a column in your result set, like last_message (instead of forum_messages.date)

So:

SELECT forum_categories.title, forum_messages.author, forum_messages.date AS last_message,            MAX(forum_messages.date)
FROM forum_categories
JOIN forum_topics ON forum_topics.category_id=forum_categories.id 
JOIN forum_messages ON forum_messages.topic_id=forum_topics.id
WHERE forum_categories.id=6
GROUP BY forum_categories.title, forum_messages.author
ORDER BY last_message ASC

Upvotes: 0

Related Questions