我的表结构是
CREATE TABLE IF NOT EXISTS `tbl_user_result` (
`Id` int(11) NOT NULL AUTO_INCREMENT,
`ResultId`int(11),
`PaperId` int(11) ,
`Title` varchar(45),
`TotalQuestions` int(11) ,
`Attempt` int(11) ,
`Correct` int(11) ,
`Wrong` int(11) ,
`Score` int(45) ,
`Minutes` int(11) ,
`TimeSt` varchar(45) ,
`UserEmail` varchar(45) ,
`UserName` varchar(45) ,
PRIMARY KEY (`Id`));
我尝试了以下SQL查询
SELECT
id,
PaperId,
Title,
Score,
Minutes,
(SELECT
COUNT(*) + 1
FROM
tbl_user_result
WHERE
Score > x.Score AND Minutes > x.Minutes) AS Rank
FROM
`tbl_user_result` as x
最佳答案
最简单的方法是使用变量:
select r.*
from (select r.*, (@rn := @rn + 1) as rank
from tbl_user_result r cross join (select @rn := 0) vars
order by score desc, minutes asc
) r;
如果没有重复项,则此方法有效。
我认为以下是您想要的查询版本:
SELECT id, PaperId, Title, Score, Minutes,
(SELECT COUNT(*) + 1
FROM tbl_user_result r2
WHERE r2.Score > r.Score OR
(r2.Score = r.Score AND r2.Minutes < r.Minutes)
) AS Rank
FROM tbl_user_result r;
关于php - 如何在MySql Server中根据最高分数和最低分钟数来计算排名,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/28831300/