Selecting Top 50 High Point Scorers

Discussion in 'PHP' started by papa_face, Oct 30, 2009.

  1. #1
    Hey :)
    I am trying to work out a way of selecting the top 50 high point scorers from my table. At the moment I have:
    		$gettop = $ftcdb->get_col("SELECT * FROM `xperience_stats` ORDER BY `points_xperience` DESC LIMIT 0, 50");
    		foreach ($gettop as $userid)
    			{
    			if ($userid == $this->getUserId())	$allowskip = true;
    			}
    Code (markup):
    It works absolutely fine. It selects the first 50 leaders and if their userid is one of the top 50 pulled from the table then $allowskip = true.
    But what happens if the 50th and 51st user have the same score? User 50 would be $allowskip = true but 51 wouldn't.
    Any idea how to get around that? With one query ideally.

    Any help would be appreciated :)
     
    papa_face, Oct 30, 2009 IP
  2. organicCyborg

    organicCyborg Peon

    Messages:
    330
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #2
    You may be able to use a sub-query, and still be able to pull it off with one query. But I don't think it would work for my suggestion.

    My idea:
    Select the top 50 scores, then use the MIN() mySQL function to return the lowest score, then select all the scores from the table that are >= to the minimum top 50 score.
     
    organicCyborg, Oct 30, 2009 IP
  3. papa_face

    papa_face Notable Member

    Messages:
    2,237
    Likes Received:
    67
    Best Answers:
    1
    Trophy Points:
    285
    #3
    Thanks for your help. I figured it out. I just basically did what you suggested. Selected all users with points equal to the 50th user.
    Thanks :)
     
    papa_face, Oct 30, 2009 IP
  4. organicCyborg

    organicCyborg Peon

    Messages:
    330
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Were you able to fit it into one query?

    A possible better solution would be to create a view with the top 50 scores, and use that to select the bottom score. Then, using that view in a subquery you could meet your requirements.
     
    organicCyborg, Oct 30, 2009 IP
  5. papa_face

    papa_face Notable Member

    Messages:
    2,237
    Likes Received:
    67
    Best Answers:
    1
    Trophy Points:
    285
    #5
    I did this:
    		$gettop = $ftcdb->get_results("SELECT `userid`,`points_xperience` FROM `xperience_stats` ORDER BY `points_xperience` DESC LIMIT 0, 50");
    		foreach ($gettop as $k => $v)
    			{
    			echo $k ." ". $v->userid . " => ".$v->points_xperience . "<br />";
    			}
    			echo "*-*-*-*-<br />";
    		$get50thscore = $ftcdb->get_results("SELECT `userid`,`points_xperience` FROM `xperience_stats` WHERE `points_xperience` = '".$gettop[49]->points_xperience."' ",0);
    		foreach ($get50thscore as $k => $v)
    			{
    			echo $k ." ". $v->userid . " => ".$v->points_xperience . "<br />";
    			}
    ?>
    Code (markup):
     
    papa_face, Oct 31, 2009 IP