如何分页MySql排名查询

时间:2014-11-24 09:09:54

标签: php mysql pagination

我有以下排名查询正常工作,每页限制25条记录,它显示前25条记录的第1至第25条,但是当我转到下一页时,排名保持相同的第1位 - 第25位应该说26日至50日。

("SELECT image, name, wins, losses, level, xp, wins
 AS W, losses AS L, TRUNCATE(wins/IF(wins=0,1,wins+losses)*100,0)
  AS PCT, @curRank := @curRank + 1 AS rank FROM teams p, 
  (SELECT @curRank := 0) r WHERE `console` = 3 AND `game` = 23
   AND `laddertype` = 1 ORDER BY xp 
   DESC LIMIT " . mysql_real_escape_string($limit) . ", 25");

//get page
$page = 0;
if (isset($_GET['id'])) {           
    $rank = mysql_query("SELECT image, name, wins, losses, level, xp, wins AS W, losses AS L, TRUNCATE(wins/IF(wins=0,1,wins+losses)*100,0) AS PCT, @curRank := @curRank + 1 AS rank FROM `teams` p, (SELECT @curRank := 0) r WHERE `console` = 3 AND `game` = 23 AND `laddertype` = 1 AND `id` = ".mysql_real_escape_string($_GET['id'])." LIMIT 1");
    $rank = mysql_fetch_assoc($rank);
    $rank = $rank['rank'];
    $page = ceil($rank/25)-1;
}
if (isset($_GET['page'])) {
    $page = (int)$_GET['page'];
}
$limit = $page * 25;



//do pagination
$teamcount = $teamtotal['COUNT(*)'];
if (isset($_GET['id'])) {
    $urlstring = '//website.com/?id='.$_GET['id'];
}else{
    $urlstring = '//website.com/';
}
$template['PAGINATION'] = getpages($urlstring, (ceil($teamcount / 25) - 1), $page);

如何让排名列随着分页而改变?

1 个答案:

答案 0 :(得分:1)

我想通了,我需要将(SELECT @curRank := 0)改为此(SELECT @curRank := $limit)