joe92 Posted April 18, 2011 Share Posted April 18, 2011 I'm trying to create a basic forum to pin onto my site. I've hit a problem with the page which displays all the threads of a certain board. Similarly to phpBB and many others, I want the threads/topics to be ordered by the latest post. I am having trouble with this query //get all the thread id's from this board $thread_query = mysql_query("SELECT DISTINCT threadID FROM forum_posts WHERE boardID = '$bid' ORDER BY postTime"); The above query only gets the 1st unique threadID and ignores the rest meaning the order is incorrect. How can I rewrite this query so it will select the unique thread ID's, but order the thread ID's by the latest postTime? Cheers, Joe Link to comment https://forums.phpfreaks.com/topic/234061-order-by-latest-post-conundrum/ Share on other sites More sharing options...
Muddy_Funster Posted April 18, 2011 Share Posted April 18, 2011 Remove the word DISTINCT from the query and - ORDER BY postTime DESC. curious though - why are you trying to re-invent the wheel so to speek? Link to comment https://forums.phpfreaks.com/topic/234061-order-by-latest-post-conundrum/#findComment-1203004 Share on other sites More sharing options...
kickstart Posted April 18, 2011 Share Posted April 18, 2011 Hi Do you just have a table of posts, or do you have a table of threads as well? $thread_query = mysql_query("SELECT threadID, MAX(postTime) AS MaxPostTime FROM forum_posts WHERE boardID = '$bid' GROUP BY threadID ORDER BY MaxPostTime"); All the bst Keith Link to comment https://forums.phpfreaks.com/topic/234061-order-by-latest-post-conundrum/#findComment-1203011 Share on other sites More sharing options...
PFMaBiSmAd Posted April 18, 2011 Share Posted April 18, 2011 only gets the 1st unique threadID ^^^ I'm going to guess that your php code isn't looping over the result set, but is instead just fetching the first row from the result set. Link to comment https://forums.phpfreaks.com/topic/234061-order-by-latest-post-conundrum/#findComment-1203018 Share on other sites More sharing options...
joe92 Posted April 18, 2011 Author Share Posted April 18, 2011 $thread_query = mysql_query("SELECT threadID, MAX(postTime) AS MaxPostTime FROM forum_posts WHERE boardID = '$bid' GROUP BY threadID ORDER BY MaxPostTime"); Thank you very much! Worked a treat. I just needed to add a DESC to the end to get my desired result: $thread_query = mysql_query("SELECT threadID, MAX(postTime) AS MaxPostTime FROM forum_posts WHERE boardID = '$bid' GROUP BY threadID ORDER BY MaxPostTime DESC"); [attachment deleted by admin] Link to comment https://forums.phpfreaks.com/topic/234061-order-by-latest-post-conundrum/#findComment-1203021 Share on other sites More sharing options...
joe92 Posted April 18, 2011 Author Share Posted April 18, 2011 p.s. How do I mark this as solved? Never mind, found it. Link to comment https://forums.phpfreaks.com/topic/234061-order-by-latest-post-conundrum/#findComment-1203023 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.