分组时出现问题

发布于 2024-11-05 17:59:28 字数 1812 浏览 0 评论 0原文

我有这个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

吾家有女初长成 2024-11-12 17:59:28

试试这个(让我知道它是否有效):

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

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
才能让你更想念 2024-11-12 17:59:28

您需要在 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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文