对于数据,我们就会涉及到统计报表,对于成绩数据,需要对统计报表进行排名
怎么对一个表中根据某个字段排名,获取名次呢?

select * from 
(SELECT
t1.*,@rank :=@rank + 1 AS pm
FROM
(
SELECT
*
FROM
t_athlete_score
where

fk_comp_id=55
ORDER BY
f_rank_score
) AS t1,
(SELECT @rank := 0) AS t2
) as m
where f_card_num='3713XXXXX53323'

获取的排名数据为:

mysql 根据某个字段排名,获取名次_字段

第一步:先对整个表进行排序

SELECT
*
FROM
t_athlete_score
where

fk_comp_id=55
ORDER BY

第二步:使用变量计算排名

SELECT
t1.*,@rank :=@rank + 1 AS pm
FROM(
...) as t1,
(SELECT @rank := 0) AS

第三步:将查询结果再次封装,然后给出响应的查询条件。

select * from 
(
查询结果
) as m
where f_card_num='3713XXXXX53323'