分组时出现问题
我有这个 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
现在,我想使用最大日期对其进行分组。所以查询变成:
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
完美!获取的日期正确: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
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
试试这个(让我知道它是否有效):
这是第二个选项:
Try this one (let me know if it works):
Here is a second option:
您需要在 GROUP BY 子句中包含 forum_categories.title 和 forum_messages.author,因为它们不是聚合函数。将 GROUP BY 视为表示结果集中的哪些值应该“挤”到一行中。
此外,您很可能希望按结果集中的列进行排序,例如last_message(而不是forum_messages.date)
所以:
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: