Click to See Complete Forum and Search --> : Ordering SELECT results by a different table
Hen Asraf
04-01-2009, 03:05 PM
I have a threads table, and a posts (replies) table. Every post has a `parent` thread. How do I order the list of threads, according to the latest reply? So, let's say I have thread a and b. Thread b was created after thread a, but thread a has a reply that was created after the last reply in thread b, so thread a should be selected first of the two.
What's the query I should use to do that?
nonamepub
04-01-2009, 03:28 PM
Do your posts have a timestamp associated with them?
Hen Asraf
04-01-2009, 04:30 PM
Yeah. Both threads and posts do, and I order them by that.
xvszero
04-02-2009, 03:57 PM
Hmm. I think you would need to select max (timestampfield) from your replies table and group by the id of the parent message. Then join the parent table on that id as well. I think that would work.
But another way to do it, which is how I do it, is just add a new field to the parent thread table "lastreply" or whatever and update it every time anyone posts a reply to that thread. Which, now that I think about it, is just adding useless overhead. But it is easier to (mentally) follow and then you don't have to worry about joining the replies table on the main forum page.