这个sql语句:列出各门课程成绩最好的两位学生?

表:
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[test]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [dbo].[test]
GO

CREATE TABLE [dbo].[test] (
 [id] [int] IDENTITY (1, 1) NOT NULL ,
 [subject] [varchar] (50) COLLATE Chinese_PRC_CI_AS NULL ,
 [score] [int] NULL
) ON [PRIMARY]

--说明:id,学生ID,subject 课程,score分数
GO


insert into test

select '1',89 union all
select '1',80 union all
select '2',100 union all
select '2',77 union all
select '2',78 union all
select '3',92 union all
select '4',69 union all
select '4',78 union all
select '1',86 union all
select '2',81 union all
select '3',90 union all
select '3',66 union all
select '2',79



答案已经有了:
select distinct t1.*
from test t1
where t1.id in
(select top 2 test.id from test where subject = t1.subject order by score desc)
order by t1.subject

但是,我不太明白 select top 2 test.id from test where subject = t1.subject order by score desc  到底是怎么意思?逻辑上结合where .. in .. 怎样实现的? 本人比较笨,请老大们详细指点,谢了!

转载于:https://www.cnblogs.com/silva/archive/2005/07/19/195802.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值