select name,  (select count(*) from 表 
     where score>=0 and score<30 and score>a.score) as 名次
from 表 a where a.score>=0 and a.score<30
union
select name, (select count(*) from 表 
   where score>=30 and score<60 and score>a.score) as 名次
from 表 a where a.score>=30 and a.score<60
union
select name,(select count(*) from 表 
   where score>=60 and score<=100 and score>a.score) as 名次
from 表 a where a.score>=60 and a.score<=100

解决方案 »

  1.   

    select name,  (select count(*) from 表 
         where score>=0 and score<30 and score>=a.score) as 名次
    from 表 a where a.score>=0 and a.score<30
    union
    select name, (select count(*) from 表 
       where score>=30 and score<60 and score>=a.score) as 名次
    from 表 a where a.score>=30 and a.score<60
    union
    select name,(select count(*) from 表 
       where score>=60 and score<=100 and score>=a.score) as 名次
    from 表 a where a.score>=60 and a.score<=100
      

  2.   


    create table ai(name varchar(100),score tinyint)
    insert into ai select 'a1a',10
    insert into ai select 'b1a',100
    insert into ai select 'c1a',90
    insert into ai select 'd1a',60
    insert into ai select 'e1a',70
    insert into ai select 'f11',80
    insert into ai select 'a11',40
    insert into ai select 'aaa',20
    insert into ai select 'aad',30
    insert into ai select 'aaa',50
    go
    select  (select count(name)+1 from ai where score>a.score and score>=0 and score<30) as ids1,*
    from ai as a
    where score>=0 and score<30
    union all
    select  (select count(name)+1 from ai where score>b.score and score>=30 and score<60) as ids2,*
    from ai as b
    where score>=30 and score<60
    union all
    select  (select count(name)+1 from ai where score>c.score and score>=60 and score<=100) as ids3,*
    from ai as c
    where score>=60 and score<=100