查询一个人的成绩是第几名,sql怎么写比较优化

godidea 2005-11-10 05:11:29
查询一个人的成绩是第几名,sql怎么写比较优化
表名: 成绩表
字段名1:学号
字段名2:成绩
...全文
147 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
cansum396 2005-11-10
  • 打赏
  • 举报
回复
WangZWang(阿来) 的做法不错,学习

select IsNULL(count(*),0)+1 as 名次
from 成绩表
where 成绩>(select 成绩 from 成绩表 where 姓名=?)

zlp321002 2005-11-10
  • 打赏
  • 举报
回复
select 名次=(select isnull(sum(1),0) from 成绩表 where 成绩<A.成绩)+1,*
from 成绩表 A
cansum396 2005-11-10
  • 打赏
  • 举报
回复
create table 成绩表(姓名 nvarchar(10),成绩 int)

set nocount on

insert into 成绩表(姓名,成绩)
select
'比尔。盖茨',90
union all
select
'阿加西',80
union all
select
'小罗',50
union all
select
'大姚',80
union all
select
'传奇',100

declare @name nvarchar(10)
set @name=N'大姚'

select IDENTITY(int,1,1) as 名次,* into 排序后成绩表 from 成绩表 order by 成绩 desc
select * from 排序后成绩表 where 姓名=@name

drop table 排序后成绩表
drop table 成绩表


名次 姓名 成绩
----------- ---------- -----------
4 大姚 80

WangZWang 2005-11-10
  • 打赏
  • 举报
回复
select IsNULL(count(*),0)+1 as 名次
from tablename
where 成绩>(select 成绩 from tablename where 学号=?)
520zyb 2005-11-10
  • 打赏
  • 举报
回复
select 名次=identity(1,1),学号,成绩 from 成绩表 order by 成绩 desc

34,594

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧