I am looking at created a piece of forum software that will have Semi-Threaded topic structure. I am writing sudo code for how to best handle the fetching of posts in the proposed environment. In a non-threaded environment the posts are selected by post_id as post ID would be auto incremented and thus could be ordered simply to get posts from newest to oldest or vis versa.
SELECT * from posts_tables
WHERE topic_id = X
ORDER by post_id ASC
LIMIT 20
In a semi threaded environment you would want to also select posts in the same manner to get the latest posts for a thread, but you would also want to select the replies that each of those posts had. Which is simply done with 2 SQL calls, but I was wondering if perhaps a better method exists only using one. With 3 Indexed fields, being post_id, topic_id and replied_to_post_id.
This time you get these results and select again afterwards
SELECT * FROM posts_tables
WHERE topic_id = X
ORDER by post_id ASC
LIMIT 20
GET RESULTS THEN
SELECT * FROM posts_table
WHERE replied_to_post_id = X,Y,Z
Is there a better manner of doing this?
If you don't understand Semi-Threaded topic structure is exactly, then below link might be of great use to use. http://www.bulletinboards.com/ThreadHelp.cfm