This is for a custom forum I am working on. i need to select all threads, get the number of posts in each thread and also the last post in each thread. I can get the number of posts, but my query is returning the first post rather than the last.
SELECT thread_id, thread_title, p.*, COUNT(p.post_id) AS Posts
FROM forums_threads
JOIN forums_posts AS p ON post_thread_id=thread_id
WHERE thread_forum_id=84
GROUP BY thread_id
ORDER BY thread_date DESC, post_date DESC
As @mixman suggests, you need to link to forum_posts twice: once to get the aggregate amounts (post count and maximum post date), and once to get the actual content of that last post (and I’m assuming that by “last” you mean “most recent”) in the thread: