我有这个MySql查询:
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
输出结果如下:
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
现在,我想使用MAX日期对其进行分组。所以查询变为:
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!所用日期是正确的: 2011-11-05 23:12:22 (该分组的最大日期)。但我也从这种行为中得到了该行的相应用户(在示例中 pierre )。
但需要另外一个。
为什么呢?它没有选择整行?我该如何解决这个问题呢?干杯
修改
事实上,我应该将此分组应用于整个查询:
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
答案 0 :(得分:3)
尝试这个(让我知道它是否有效):
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
这是第二个选择:
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
答案 1 :(得分:0)
您需要在GROUP BY子句中包含forum_categories.title和forum_messages.author,因为它们不是聚合函数。想想GROUP BY说的是结果集中的哪些值应该“一起”被压缩成一行。
此外,您很可能希望按结果集中的列进行排序,例如last_message(而不是forum_messages.date)
所以:
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