表结构如下:Student

一个student表有姓名和成绩两个字段。查询某个学生的姓名和成绩以及排名。

要求得到的结果如下:

一个student表有姓名和成绩两个字段。查询某个学生的姓名和成绩以及排名。

sql语句如下:

 SELECT   1+(SELECT   COUNT(distinct   score)    
                            FROM   (select   *  
                                            from   student)   AS   T1    
                          WHERE   score   >   T2.score)   as   名次   ,[name],score  
        FROM   (select   *  
                        from   student 
                    )   AS   T2     ORDER   BY   T2.score   desc

 

一个student表有姓名和成绩两个字段。查询某个学生的姓名和成绩以及排名。

 

sql如下:

 SELECT   1+(SELECT   COUNT(distinct   score)    
                            FROM   (select   *  
                                            from   student)   AS   T1    
                          WHERE   score   >   T2.score)   as   名次   ,[name],score  
        FROM   (select   *  
                        from   student  
                    )   AS   T2     where T2.[name]='jtome1'
  ORDER   BY   T2.score   desc

 

 

精简版本如下:

select t1.[name],t1.score,(select count(distinct score)+1 from student as t2 where score>t1.score) as 名次
from student as t1
where t1.[name] ='jtome1'
order by t1.score desc;

相关文章: