2012-02-29 63 views
0

可能重复的行/列:
High score system from my iphone game选择标识,并显示从中

我的查询是:

$sql = "SELECT * FROM $table WHERE "; 

    switch($type) { 
     case "global": 
      $sql .= "1 "; 
      break; 
     case "device": 
      $sql .= "udid = '$udid' "; 
      break; 
     case "name": 
      $sql .= "name = '$name' "; 
      break; 
    } 

    $sql .= "ORDER BY $sort "; 
    $sql .= "LIMIT $offset,$count "; 

    $result = mysql_query($sql,$conn); 

的UDID是一个唯一的标识符。而循环:

while ($row = mysql_fetch_object($result)) { 
       echo '<tr> 
         <td> 
         '.$rank.' 
         </td> 
               <td> 
         '.$row->name.' 
         </td> 
         <td> 
         '.$row->score.' 
         </td> 
             <td> 
         '.$row->udid.' 
         </td> 

         </tr>'; 
           $rank++; 
      } 

这显示了一个表如下:

rank  name    score    udid 
------------------------------------------------------ 
1   Joe    2.30101   49beeb956274dfb8406d66f7403dd4bf3d9c6da9 
2   John    2.41940   119d62e398bf6f1cea89b235b5c8ecf95255442e 
3   Frank    2.64561   another id 
4   Mitt    3.00101   another id 
5   Sean    3.10001   another id 

这工作得很好,但如果我想给出一个对UDID的分数和排名,所以如果我想的UDID :119d62e398bf6f1cea89b235b5c8ecf95255442e我想获得排名2,名字约翰和得分2.41940。

我该怎么做?

+0

如果您运行相同的查询,但将LIMIT $ offset,$ count改为LIMIT,则会发生什么情况($ offset + $ rank)。“,1'? – Travesty3 2012-02-29 20:04:07

+0

或实际上'($ offset + $ rank - 1)' – Travesty3 2012-02-29 20:05:30

回答

1

获取得分与UDID查询该行的并使用以下查询来获取排名

$sql = "SELECT Count(*) FROM $table WHERE score < '$score'"; 

的排名是从上述查询返回的值+ 1