create table tb
(
姓名 varchar(10),
班级 int,
分数 int,
名次 int
)
insert into tb
select
'zhaoying' , 1,60,0 union select
'douye' , 1,58,0 union select
'machao' , 1,58,0 union select
'liujie' , 1,54,0 union select
'zhangqiang' , 2,54,0 union select
'machuang' , 2,66,0
update T set 名次=(select count(1) from tb where 班级 = T.班级 and 分数>T.分数)+1 from tb T
select * from tb order by 班级,名次
/*
姓名 班级 分数 名次
zhaoying 1 60 1
douye 1 58 2
machao 1 58 2
liujie 1 54 4
machuang 2 66 1
zhangqiang 2 54 2
*/
drop table tb