表结构如下: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
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;