mysql 排序并增加序号,分数相同并列
时间:2018-05-15 11:03:51
收藏:0
阅读:1125
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
结果:
原文:https://www.cnblogs.com/janeaiai/p/9039843.html
评论(0)