【发布时间】:2014-07-26 19:38:17
【问题描述】:
我有两个表:topic 和 message。
表topic:
topic_id
// some other not important columns here
表message:
message_id
topic_id
creation_date
// some other not important columns here
如您所见,每个主题可以有许多消息。
我想要获取的是:所有主题的列表(包含所有主题列)以及每个主题的消息计数,按属于主题的最新消息排序(最近消息在顶部的主题)。
这是我的尝试:
SELECT topic.*, COUNT(message.message_id)
FROM topic LEFT OUTER JOIN message ON topic.topic_id = message.topic_id
GROUP BY topic.topic_id
ORDER BY message.creation_date DESC
这显然行不通。我将不胜感激。
【问题讨论】:
-
错误信息是什么?
标签: sql postgresql