freaksauce Posted November 2, 2007 Share Posted November 2, 2007 Each score submitted inserts a new row, I have the following query to order the users scores by the sum of their scores. SELECT *, SUM(score) as sum FROM `scores` GROUP BY user_id ORDER BY sum DESC Now I need some way of getting their rank by counting the number of rows greater than the score. So something like: SELECT *, SUM(score) as sum, COUNT(sum) as rank FROM `scores` GROUP BY user_id WHERE sum > 100 ORDER BY sum DESC Does that make any sense? Server version: 5.0.24a-community PHP Version 5.1.6 Quote Link to comment Share on other sites More sharing options...
fenway Posted November 2, 2007 Share Posted November 2, 2007 Check out this resource (it was in one of the stickies). Quote Link to comment Share on other sites More sharing options...
Barand Posted November 3, 2007 Share Posted November 3, 2007 <?php mysql_query ("CREATE TEMPORARY TABLE tmpx SELECT user_id, SUM(score) as score FROM ratings GROUP BY user_id"); mysql_query ("CREATE TEMPORARY TABLE tmpy SELECT user_id, SUM(score) as score FROM ratings GROUP BY user_id"); $sql = "SELECT x.user_id, x.score, (SELECT COUNT(*)+1 FROM tmpy WHERE score > x.score) as position FROM tmpx x ORDER BY position"; $res = mysql_query($sql) or die (mysql_error()."<p>$sql</p>"); while (list ($pid, $sc, $pos) = mysql_fetch_row($res)) { echo "$pid ($sc) : $pos <br/>"; } ?> Quote Link to comment 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.