Grab only the last post from all topics in a category from phpbb
Question
I want to only grab the last post of all topics in a category (Category->Forum->Topic->Post) from a phpbb database in a single query. Currently I have cooked up this, but it returns only the first post, not the last.
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
Does anybody know how to correctly do this?
Solution
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 (…)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow