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

猿来是我 2005-07-20 08:52:07
表:
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 .. 怎样实现的?请老大们详细指点,谢了!

...全文
993 16 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
lsqkeke 2005-11-24
  • 打赏
  • 举报
回复
支持楼主
yongwin 2005-11-24
  • 打赏
  • 举报
回复
where subject = t1.subject 这个条件可以帮助楼主解决你的疑惑,这个条件实质是相当去group by subject 了,楼主可以细观察一下,就可以发现通到此条件已经实现了对subject 的分组所以它返回的条件就不会是楼主认为的二条了,注:楼主所写的程序不能用group by 代替where subject = t1.subject
while1228 2005-11-24
  • 打赏
  • 举报
回复
mark.
na.nashi 2005-11-24
  • 打赏
  • 举报
回复
相关子查询,子查询里面的t1.subject是父查询的字段。抽出父查询的第一行记录,到子查询里去扫描,按subject分组选出记录。每组记录按score降序排列,就可以较最高2组的id返给父查询。再找父查询的第二行记录........
mengzulin 2005-07-21
  • 打赏
  • 举报
回复
Microsoft是这样解释的.
在一个相关(或重复)子查询中使用 IN,该查询的值取决于外部查询。它被重复执行,为外部查询
可能 选择的每行各执行一次。
猿来是我 2005-07-21
  • 打赏
  • 举报
回复
to filebat(Mark) :

select top 2 test.id from test where subject = t1.subject order by score desc
我的理解是:这个子查询只查询到2条数据啊!! 只能查到一个科目的? 好像不能查到所有科目的情况!
猿来是我 2005-07-20
  • 打赏
  • 举报
回复
select top 2 test.id from test where subject = t1.subject order by score desc
我的理解是:这个子查询只查询到2条数据啊!! 只能查到一个科目的? 好像不能查到所有科目的情况!
mengzulin 2005-07-20
  • 打赏
  • 举报
回复
这是一个子查询.意思是找出子查询subject等于父查询(也就是另名是t1)的subject的前2个id
冷箫轻笛 2005-07-20
  • 打赏
  • 举报
回复
一条记录一条记录的遍历所有记录
我想楼主应该会明白这个语句了
mschen 2005-07-20
  • 打赏
  • 举报
回复
是一个相关子查询,就相当于查询某个科目下的成绩最好的两个人的id号.
filebat 2005-07-20
  • 打赏
  • 举报
回复
to 楼上, 你那个太麻烦了.
楼主的那个方法简单.

觉得楼主的那个方法很清楚啊.
select top 2 test.id from test where subject = t1.subject order by score desc
就是找出每一课前两名学生的ID, 然后再将它显示出来.
powerly 2005-07-20
  • 打赏
  • 举报
回复
sorry! 没有看清题目
稍微改一下:
select distinct a.* from test a
inner join
(
select subject,max(score) as score from test group by subject
union all
select subject,max(score) as score from test
where score not in (select max(score) as score from test group by subject)
group by subject
) b
on a.subject = b.subject and a.score = b.score
有更好的请改进!!
summerICEREDTEA 2005-07-20
  • 打赏
  • 举报
回复
powerly(豆豆)
你的只能取得一条
猿来是我 2005-07-20
  • 打赏
  • 举报
回复
to powerly(豆豆) :
你的这个只取出了每科的一名最好成绩,,要求是前两名!
powerly 2005-07-20
  • 打赏
  • 举报
回复
楼主的sql太难理解了!这个也能实现 比较直观!
select distinct a.* from test a
inner join
(
select subject,max(score) as score from test group by subject
) b
on a.subject = b.subject and a.score = b.score
猿来是我 2005-07-20
  • 打赏
  • 举报
回复
up

27,582

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 应用实例
社区管理员
  • 应用实例社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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