Mysql 获取排行榜

####需求:

知识竞赛从所有关卡的最好答题记录表中,获取所有关卡总分排名数据

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
SELECT
t.*,
@rownum := @rownum + 1 AS rank
FROM
( SELECT @rownum := 0 ) r,
(
SELECT
count( a.user_id ) AS user_times,
a.user_id,
sum( a.score ) AS scores,
sum( a.time_cost ) AS time_costs,
u.nick,
u.mobile,
u.avatar
FROM
contest_best_logs AS a
LEFT JOIN users AS u ON a.user_id = u.id
WHERE
a.act_id IN ( SELECT pass_act_id AS act_id FROM contestpro_passes AS b WHERE b.act_id = '10366' )
GROUP BY
a.user_id
ORDER BY
scores DESC, time_costs ASC, user_times DESC
LIMIT 0,50
) AS t

获取的结果如下:

image-20200730093455129

请我喝杯咖啡吧~

支付宝
微信