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
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.
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
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.
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):