jackpf Posted May 8, 2009 Share Posted May 8, 2009 Hi all, Once again, for my forum, I'm having trouble with this query. I'm trying to get "active users" in a thread, much like this forum does. I have the query working, but I would also like to retrieve the number of posts in this thread by each user, and order users depending on how many posts they have. This is my query thus far: "SELECT DISTINCT F.`Author`, COUNT(T.`ID`) FROM `$tb_Forum` F LEFT OUTER JOIN `$tb_Forum` T ON F.`Author`=T.`Author` AND F.`ID`=T.`Thread` WHERE F.`ID`='$threadid' OR F.`Thread`='$threadid';" The `Author` is the author of the post, and $threadid is the ID of the current thread. However, this is only returning one random author, and I don't understand why. If I remove COUNT(T.`ID`) it works fine, but obviously doesn't return the number of posts. Any help would be greatly appreciated, Cheers. Jack. Quote Link to comment https://forums.phpfreaks.com/topic/157375-solved-mysql-join-one-result/ Share on other sites More sharing options...
Mchl Posted May 8, 2009 Share Posted May 8, 2009 Remove DISTINCT and add GROUP BY F.`Author` Quote Link to comment https://forums.phpfreaks.com/topic/157375-solved-mysql-join-one-result/#findComment-829590 Share on other sites More sharing options...
JonnoTheDev Posted May 8, 2009 Share Posted May 8, 2009 Do you not have an authors table? You would achieve this in the following mannor: SELECT a.author, COUNT(f.id) AS totalPosts FROM forum f LEFT JOIN authors a ON (a.authorId=f.authorId) WHERE f.threadId='123' GROUP BY a.authorId ORDER BY totalPosts DESC Quote Link to comment https://forums.phpfreaks.com/topic/157375-solved-mysql-join-one-result/#findComment-829595 Share on other sites More sharing options...
jackpf Posted May 8, 2009 Author Share Posted May 8, 2009 "SELECT F.`Author`, COUNT(T.`ID`) AS `PostCount` FROM `$tb_Forum` F LEFT OUTER JOIN `$tb_Forum` T ON F.`Author`=T.`Author` AND F.`ID`=T.`ID` WHERE F.`ID`='$threadid' OR F.`Thread`='$threadid' GROUP BY F.`Author` ORDER BY `PostCount` DESC;" Oh dude, cheers Mchl Works perfectly. Even orders by posts as well Thanks for the reply as well neil.johnson. Nice Quote Link to comment https://forums.phpfreaks.com/topic/157375-solved-mysql-join-one-result/#findComment-829633 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.