2013-02-12 123 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代码中更容易。