码迷,mamicode.com
首页 > 数据库 > 详细

mysql 排序并增加序号,分数相同并列

时间:2018-05-15 11:13:15      阅读:1689      评论:0      收藏:0      [点我收藏+]

标签:rownum   sele   序号   SQ   FN   相同   分数   where   info   

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 排序并增加序号,分数相同并列

标签:rownum   sele   序号   SQ   FN   相同   分数   where   info   

原文地址:https://www.cnblogs.com/janeaiai/p/9039843.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!