代码如下所示: SELECT @i:=@i+1 rowNum, if(@total=t.s_score,@rank,@rank:=@i) rank,@total:=t.s_score, t.* from( select t1.* ,t2.s_score from student t1 LEFT JOIN score t2 on t1.s_id=t2.s_id and t2.c_id="01" ORDER BY t2.s_score desc )t,(select @i:=0,@rank:=0,@total:=null) s ; SELECT @i:=@i+1 rowNum, if(@tota