Keleth
09-21-2009, 05:08 PM
I was hoping I could get some help with something...
Basically, in my ever ongoing battle to learn mySQL, I'm trying to make a forum of sorts. What I've got is a table of threads, which has 3 columns (threadID, parentForumID, locked), and a table of posts (postID, threadID, title, authorID, message, datePosted, lastEdited, timesEdited). The current table of threads has 2 rows, the table of posts has 3 rows at the moment. In terms of relationships, every row in posts will have one corresponding row in threads, and every row in threads will have one or more corresponding rows in posts.
What I'd like to do is pull all the threads with the latest post information, so I can both sort by that latest post (newest threads at top), as well as displaying the information from the latest post. I came up with the following query:
SELECT threads.threadID, threads.locked, posts.title
FROM threads
LEFT JOIN (SELECT * FROM posts ORDER BY datePosted DESC) posts
ON threads.threadID = posts.threadID
WHERE threads.forumID = 1
ORDER BY posts.datePosted DESC
When I do this, though, I get all 3 post rows with the thread info attached, instead of 2 thread rows with the latest posts. I can't figure out if I'm doing the JOIN wrong or if I'm doin something else wrong.
I know I can use GROUP BY, and came up with this:
SELECT threads.threadID, threads.locked, posts.title
FROM threads
LEFT JOIN (SELECT * FROM posts ORDER BY datePosted DESC) posts
ON threads.threadID = posts.threadID
WHERE threads.forumID = 1
GROUP BY threads.threadID
ORDER BY posts.datePosted DESC
It works, but I'd like to figure out if this is the right way to do it and if my JOIN is right/wrong.
Also, I was wondering if there is a way to count the number of posts for each thread from the posts table. Right now, I'm doing all the processing by having a separate query for each thread, which means if there happen to be 20 threads, thats 20 extra queries going on, which I should obviously avoid.
Basically, in my ever ongoing battle to learn mySQL, I'm trying to make a forum of sorts. What I've got is a table of threads, which has 3 columns (threadID, parentForumID, locked), and a table of posts (postID, threadID, title, authorID, message, datePosted, lastEdited, timesEdited). The current table of threads has 2 rows, the table of posts has 3 rows at the moment. In terms of relationships, every row in posts will have one corresponding row in threads, and every row in threads will have one or more corresponding rows in posts.
What I'd like to do is pull all the threads with the latest post information, so I can both sort by that latest post (newest threads at top), as well as displaying the information from the latest post. I came up with the following query:
SELECT threads.threadID, threads.locked, posts.title
FROM threads
LEFT JOIN (SELECT * FROM posts ORDER BY datePosted DESC) posts
ON threads.threadID = posts.threadID
WHERE threads.forumID = 1
ORDER BY posts.datePosted DESC
When I do this, though, I get all 3 post rows with the thread info attached, instead of 2 thread rows with the latest posts. I can't figure out if I'm doing the JOIN wrong or if I'm doin something else wrong.
I know I can use GROUP BY, and came up with this:
SELECT threads.threadID, threads.locked, posts.title
FROM threads
LEFT JOIN (SELECT * FROM posts ORDER BY datePosted DESC) posts
ON threads.threadID = posts.threadID
WHERE threads.forumID = 1
GROUP BY threads.threadID
ORDER BY posts.datePosted DESC
It works, but I'd like to figure out if this is the right way to do it and if my JOIN is right/wrong.
Also, I was wondering if there is a way to count the number of posts for each thread from the posts table. Right now, I'm doing all the processing by having a separate query for each thread, which means if there happen to be 20 threads, thats 20 extra queries going on, which I should obviously avoid.