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 排序并增加序号,分数相同并列的主要内容,如果未能解决你的问题,请参考以下文章

mysql 多重排序

mysql高阶语句

mysql中给查询结果添加序号列

MySQL数据库语句

mysql5.7 mysql8窗口函数分组排序并在组内编号

Java 数组元素的排序,输出排序后的序号,从小到大排序,并且要求相同的数据其序号是相同的。