mysql 排序并增加序号,分数相同并列
Posted JaneAi
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了mysql 排序并增加序号,分数相同并列相关的知识,希望对你有一定的参考价值。
1,排序,根据总分 增加序号
表结构:
sql:
SET @row=0;
SELECT b.*, @row:=@row+1 rownum FROM (
select *,SUM(a.achievement) as achievementAll from cp_cj as a
where 1=1 GROUP BY a.studentNum order by SUM(a.achievement) DESC limit 0,30
) as b
结果:
2,排序,根据总分 增加序号,并且按照总分相同的并列
sql:
select c.* ,ifnull((
select count(*) from
(
select *,SUM(a.achievement) as achievementAll from cp_cj as a
where 1=1 GROUP BY a.studentNum order by SUM(a.achievement) DESC
) as b
where c.achievementAll < b.achievementAll
),0)+1 as rownum from
(
select *,SUM(a.achievement) as achievementAll from cp_cj as a
where 1=1 GROUP BY a.studentNum order by SUM(a.achievement) DESC limit 0,30
) as c
结果:
以上是关于mysql 排序并增加序号,分数相同并列的主要内容,如果未能解决你的问题,请参考以下文章