积分表:t_participant_points
1、带并列
SELECT
p.*,
CASE
WHEN @prevRank = p.total_points THEN
@curRank
WHEN @prevRank := p.total_points THEN
@curRank := @curRank + 1
END AS ranking
FROM
( SELECT total_points FROM t_participant_points ORDER BY total_points DESC ) AS p,
( SELECT @curRank := 0, @prevRank := NULL ) r
运行效果:
2、不带并列
SELECT
p.*,
CASE
WHEN @prevRank := p.total_points THEN
@curRank := @curRank + 1
END AS ranking
FROM
( SELECT total_points FROM t_participant_points ORDER BY total_points DESC ) AS p,
( SELECT @curRank := 0, @prevRank := NULL ) r
运行效果: