1

So I have a SQL select were it grabs the rankings of all users and then shows them in a table. But what I need to do now is grab which number the user is. So at the moment is echo's out highest to lowest but I need to find out what place that users is from the high to low.

$query = "SELECT 
    belongsto, 
    SUM(level) as sumLevel 
FROM 
    user_pokemon 
group by 
    belongsto 
order by 
    sumLevel desc
    LIMIT  $pageLimit,".PAGE_PER_NO;

I am storing the users user-name in a $_SESSION['username']; And there user-name for each monster is stored in the belongs too.

Would this work ?

 $query = "select 
    (count(*) from user_pokemon  where belongsto ='".$_SESSION['username']."' ) as position,
    belongsto,
    level
from user_pokemon
order by level;";


$res=mysql_query($query);

1 Answers1

0

In a subquery, find the user's ranking. Then find count of all users having ranking greater than equal to the subquery's ranking. This should give you the position of user.

  • how would i go about doing that ? – Billy White Jul 28 '13 at 21:14
  • Sorry for replying late, I had turned in after waiting for a while. I suggest following through the answer recommended at the top of your question as both your and that issue look similar, and it has been dealt in more detail :) –  Jul 29 '13 at 04:23