我有一个排行榜,我想给每一行排他们有多少积分。这是我到目前为止的代码。它工作正常但是当两个玩家拥有相同数量的积分时,等级是相同的,例如,如果DB中有2/2个玩家,他们都有100个积分,他们的等级都是1。
我想即使他们有相同数量的分数给予其中一个更高的等级,所以它将是1,2。
SELECT id, leaderfirst.pictureid, leaderfirst.point, FIND_IN_SET( leaderfirst.point, (
SELECT GROUP_CONCAT( leaderfirst.point
ORDER BY leaderfirst.point DESC )
FROM leaderfirst )
) AS rank
FROM leaderfirst
答案 0 :(得分:0)
leaderboard =
SELECT id,
leaderfirst.pictureid,
leaderfirst.point,
FIND_IN_SET( leaderfirst.point,
( SELECT GROUP_CONCAT(
leaderfirst.point ORDER BY leaderfirst.point DESC ,id)
FROM leaderfirst ) ) AS rank
FROM leaderfirst
试试这个。
答案 1 :(得分:0)
你想要的是SQL中的增量计数器:
有两种方法可以做到这一点:
set @rownum := 0;
然后
SELECT id, leaderfirst.pictureid, leaderfirst.point,
FIND_IN_SET( leaderfirst.point,
( SELECT GROUP_CONCAT(
leaderfirst.point ORDER BY leaderfirst.point DESC
)
FROM leaderfirst )
) AS rank,
@rownum := @rownum + 1 AS row_number
FROM leaderfirst
ORDER BY row_number
将前两个查询组合在一起,您可以使用:
SELECT id, leaderfirst.pictureid, leaderfirst.point,
FIND_IN_SET( leaderfirst.point,
( SELECT GROUP_CONCAT(
leaderfirst.point ORDER BY leaderfirst.point DESC
)
FROM leaderfirst )
) AS rank,
@rownum := @rownum + 1 AS row_number
FROM leaderfirst
CROSS JOIN (select @rownum := 0) r
ORDER BY row_number
来源:This answer here。
简单地忽略尝试使用MySQL生成此数据;你说你不关心订单,只是结果排名的自动增量性质;因此,如果您使用PHP(以及其他语言的类似工作),您可以执行类似的操作:
下面的伪代码:
$mysqlDataOutput = mysql result data from your SQL . "ORDER BY rank DESC";
$counter = 0;
foreach($mysqlDataOutput as $resultRow){
print $counter.") ".resultRow['points']. " -
<img src='".$resultRow['pictureid']."' alt=''>";
$counter++;
}
unset($mysqlDataOutput, $resultRow);
如果你想以相反的方式订购(最低排名第一),那么只需反转你的SQL ORDER BY
并设置:
$counter = count($mysqlDataOutput);
foreach(){
...
$coounter--;
}