I'm building some very simple forum software as a sort of self-test for my PHP and MySQL skills, but I'm not quite sure how to accomplish this task. I have a table called Threads
which contains a list of all threads. I also have a table called Posts
, which contains all posts. Each table's primary key is an auto-increment ID. Each row in Posts
also contains the ID of the thread it belongs to.
While I can easily retrieve all of the threads in a certain subforum with a query like this:
SELECT * FROM Threads WHERE ForumID='$forumid'
...I'm not sure how to sort that based on the latest post from each thread. I can get the posts for any given thread like this:
SELECT * FROM Posts WHERE ThreadID='$threadid'
...but I don't know how to incorporate that data into my initial query to sort it. Since each post has a unique ID, posts with higher IDs will always be more recent, so there's no need to compare dates or anything like that. I'm just unclear on how to actually do the query.
I'm pretty sure that this is possible in just MySQL, but if it's not, what would be the most efficient PHP solution? Thanks!