我想只在一个查询中从phpbb数据库中获取某个类别(Category-> Forum-> Topic-> Post)中所有主题的最后一篇文章。目前我已经熟了这个,但它只返回第一个帖子,而不是最后一个帖子。
SELECT *, MAX(p.post_id)
FROM phpbb_forums f, phpbb_topics t, phpbb_posts p
WHERE f.parent_id IN (<categories>)
AND t.forum_id = f.forum_id
AND p.topic_id = t.topic_id
GROUP BY p.topic_id
有人知道如何正确地做到这一点吗?
答案 0 :(得分:3)
SELECT *
FROM phpbb_forums f
JOIN phpbb_topics t
ON t.forum_id = f.forum_id
JOIN phpbb_posts p
ON p.post_id =
(
SELECT pi.post_id
FROM phpbb_posts pi
WHERE pi.topic_id = t.topic_id
ORDER BY
pi.date DESC
LIMIT 1
)
WHERE f.parent_id IN (…)