2013-02-12 119 views
1

嗨我想簡單地添加1,2,3,4,5,6,7,8,9,10等級。Mysql添加簡單排名表

我的輸出現在給出如下:

USER ID | SCORE 
2  | 10242 
13231 | 3427 
23732 | 3378 
24638 | 2934 
23468 | 1898 

我儘量做到的是:

RANK | USER ID | SCORE 
#1 | 2  | 10242 
#2 | 13231 | 3427 
#3 | 23732 | 3378 
#4 | 24638 | 2934 
#5 | 23468 | 1898 

這是我的PHP:

<?php $result = mysql_query("SELECT * FROM `users_score` ORDER BY `users_score`.`score` DESC LIMIT 0 , 10") or die(mysql_error()); 
if(mysql_num_rows($result) > 0): ?> 
<table> 
    <tr> 
     <th style="text-align:left;">ID</th> 
     <th style="text-align:left;">SCORE</th> 
    <tr> 
    <?php while($row = mysql_fetch_assoc($result)): ?> 
    <tr> 
     <td><?php echo $row['user_id']; ?></td> 
     <td style="font-weight: bold; color: #008AFF;"><?php echo $row['score']; ?></td> 
    </tr> 
    <?php endwhile; ?> 
</table> 
<?php endif; ?> 

有一個簡單的像數這個功能?

回答

0
If you just want changes in your php here is the code... 

<?php 
$result = mysql_query("SELECT * FROM `users_score` ORDER BY `users_score`.`score` DESC LIMIT 0 ,10") or die(mysql_error()); 

$rank=0; 
$temp_score=0; 

if(mysql_num_rows($result) > 0): ?> 
<table> 
    <tr> 
     <th style="text-align:left;">RANK</th> 
     <th style="text-align:left;">ID</th> 
     <th style="text-align:left;">SCORE</th> 
    <tr> 
    <?php while($row = mysql_fetch_assoc($result)): 
     if($temp_score!=$row['score']) 
      $rank++; 
    ?> 
    <tr> 
    <td><?php echo "#".$rank; ?></td> 
    <td><?php echo $row['user_id']; ?></td> 
    <td style="font-weight: bold; color: #008AFF;"><?php echo $row['score']; ?></td> 
    </tr> 
    <?php 
     $temp_score=$row['score]; 
     endwhile; ?> 
</table> 
<?php endif; ?> 
+0

非常感謝! – swordsecurity 2013-02-12 23:43:17

1

你可以通過使用變量來查詢。

SELECT @row:[email protected]+1 as RankNo, 
     a.UserID, 
     a.Score 
FROM tableName a, (SELECT @row:=0) b 
ORDER BY a.Score DESC 

,但它有一個缺點,它不處理扳平比分

,如果你想在排名添加#,cancatenate的RankNo with#`

UPDATE 1

SELECT CONCAT('#', @row:[email protected]+1) as RankNo, 
     a.UserID, 
     a.Score 
FROM tableName a, (SELECT @row:=0) b 
ORDER BY a.Score DESC 
1
SELECT (@rank := @rank + 1) AS rank, user_id, score 
FROM ( SELECT user_id, score 
     FROM scores, (SELECT @rank := 0) AS vars 
     ORDER BY score DESC) AS h 

SQLFiddle

+0

我不認爲嵌套查詢會獲得任何結果。我錯過了什麼嗎? – StarNamer 2013-02-12 12:23:53

+0

可能不是在這種情況下,但有一些例如「GROUP BY」,它需要排序和嵌套1st,然後SELECT。只是玩它安全,但這樣做:) – 2013-02-12 12:25:49

0

你所尋找的是一個相當於MS SQL的ROW_NUMBER()函數。這在之前已被回答,例如herehere

答案就是使用類似:

SELECT @rn:[email protected]+1 as RANK, ID, SCORE 
     FROM `users_score`, (SELECT @rn:=0) as rn 
     ORDER BY `users_score`.`score` DESC LIMIT 0 

更新

只是爲了興趣,搬運匹配得分可以在SQL中完成:

SELECT @rn := @rn + case (@ls-SCORE) when 0 then 0 else @s end as RANK, 
     ID, 
     SCORE, 
     @s := case (@ls-SCORE) when 0 then @s+1 else 1 end as STEPSIZE, 
     @ls := SCORE as LASTSCORE 
FROM `users_score`.`score` a, 
     (SELECT @rn := 0) b, 
     (SELECT @ls := max(SCORE)+1 FROM `users_score`.`score`) c, 
     (SELECT @s := 1) d 
ORDER BY a.Score DESC LIMIT 0 

然而,如果你想這樣做,那麼在PHP代碼中更容易。