我有两个MySQL表如下
表:得分
表:用户
我的要求是对得分进行排名并检索得分total_score是否大于某个值(例如,如果total_score小于15,则排名所有得分)。如果分数相等,那么也要考虑时间。
我编写了以下查询来获取包括排名在内的行(我对SQL查询并不熟悉,但我试图找到一种方法来做到这一点 - 没有运气)。
尝试 - 1
SELECT score.total_score,
CONCAT(users.user_first_name,' ', users.user_last_name) AS name,
users.user_email,
FIND_IN_SET(total_score,
(SELECT GROUP_CONCAT(total_score
ORDER BY total_score ASC)
FROM score)) AS rank
FROM score
INNER JOIN users ON score.user_id = users.user_id
WHERE score.total_score > 15
ORDER BY score.total_score DESC LIMIT 10
尝试 - 2
SET @Rank := 0;
SELECT score.total_score,score.time,
CONCAT(users.user_first_name,' ', users.user_last_name) AS name,
users.user_email,
@Rank := @Rank + 1 AS rank
FROM score
INNER JOIN users ON score.score_id = users.user_id
WHERE score.total_score > 15
ORDER BY score.total_score DESC, time ASC LIMIT 10
两次尝试均失败,简而言之,我试图做的是,
通过考虑时间(有可能获得相同分数),根据total_score对用户分数进行排名。
提前致谢。
答案 0 :(得分:0)
你可以试试这个。 ORDER BY (score.total_score/score.time)
SET @Rank := 0;
SELECT score.total_score, score.time,
CONCAT(users.user_first_name,' ', users.user_last_name) AS name,
users.user_email,
(@Rank := @Rank + 1) AS rank
FROM score
INNER JOIN users ON score.user_id = users.user_id
WHERE score.total_score > 15
ORDER BY (score.total_score/score.time) ASC LIMIT 10