verdrm Posted June 16, 2009 Share Posted June 16, 2009 Hi, I need to find the blog poster in my MySQL table with the most # of posts. How would I go about doing that? id username post timestamp Quote Link to comment https://forums.phpfreaks.com/topic/162419-find-biggest-poster/ Share on other sites More sharing options...
Maq Posted June 16, 2009 Share Posted June 16, 2009 Assuming 'post' is number of total posts. SELECT username from table ORDERY BY `post` DESC LIMIT 1 Quote Link to comment https://forums.phpfreaks.com/topic/162419-find-biggest-poster/#findComment-857287 Share on other sites More sharing options...
killah Posted June 16, 2009 Share Posted June 16, 2009 Do you currently count how many post's the user make's? If so: $query = mysql_query('SELECT `username` FROM `table` ORDER BY `post` DESC LIMIT 1', $resource_link); $user = mysql_fetch_assoc($query); echo $user['username']; Quote Link to comment https://forums.phpfreaks.com/topic/162419-find-biggest-poster/#findComment-857290 Share on other sites More sharing options...
bibliovermis Posted June 16, 2009 Share Posted June 16, 2009 Use an associative array to tally the records, then reverse sort the array while preserving the keys. $userposts=array(); $result=mysql_query ('select `username` from `table`'); while ($row = mysql_fetch_assoc($result)) $userposts[$row['username']]++; arsort($userposts); list($user, $count) = each($userposts); Quote Link to comment https://forums.phpfreaks.com/topic/162419-find-biggest-poster/#findComment-857395 Share on other sites More sharing options...
Maq Posted June 16, 2009 Share Posted June 16, 2009 Use an associative array to tally the records, then reverse sort the array while preserving the keys. $userposts=array(); $result=mysql_query ('select `username` from `table`'); while ($row = mysql_fetch_assoc($result)) $userposts[$row['username']]++; arsort($userposts); list($user, $count) = each($userposts); That's the most inefficient way to solve this problem. Why have MySQL return ALL the records when you only need 1... What if there's 80,000 users, are you going to put them in an array? Quote Link to comment https://forums.phpfreaks.com/topic/162419-find-biggest-poster/#findComment-857396 Share on other sites More sharing options...
bibliovermis Posted June 16, 2009 Share Posted June 16, 2009 That's the most inefficient way to solve this problem. Why have MySQL return ALL the records when you only need 1... What if there's 80,000 users, are you going to put them in an array? Which 1 record contains the tally? Why do you assume that the 'post' field is a count of all posts made, rather than the post itself? Why would verdrm be asking how to find the biggest poster if it was a simple matter of sorting by the 'post' field? I stated to only pull the 'username' field because of efficiency. There is no need to pull the content when you are only interested in the count. This is a PHP problem, not a MySQL problem. Quote Link to comment https://forums.phpfreaks.com/topic/162419-find-biggest-poster/#findComment-857483 Share on other sites More sharing options...
Maq Posted June 16, 2009 Share Posted June 16, 2009 Which 1 record contains the tally? I don't know, that's why I said, "Assuming 'post' is number of total posts.". Why do you assume that the 'post' field is a count of all posts made, rather than the post itself? Same reason you assumed otherwise. Because the OP didn't specify, it doesn't matter anyway, you can still extrapolate the info with a single query. I stated to only pull the 'username' field because of efficiency. There is no need to pull the content when you are only interested in the count. I'm not. Do you not understand the code I provided? You're the one pulling extra information out when you only need MySQL to select 1 username. Not only that you're putting all of them in an array and performing multiple methods on it. Very inefficient... This is a PHP problem, not a MySQL problem. Wrong. Quote Link to comment https://forums.phpfreaks.com/topic/162419-find-biggest-poster/#findComment-857495 Share on other sites More sharing options...
kickstart Posted June 16, 2009 Share Posted June 16, 2009 Hi Assuming that it is a table of posts, with `post` being the actual post and not a count:- SELECT username, count(id) AS post_count FROM `table` GROUP BY username ORDER BY post_count DESC LIMIT 0,1 All the best Keith Quote Link to comment https://forums.phpfreaks.com/topic/162419-find-biggest-poster/#findComment-857507 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.