查询1号课程成绩比2号同学该门课程成绩高的所有学生的学号

技术更新太快 2009-03-17 02:49:37
有S表,表里面有
SNO INT --主键,学生编号
SNAME --学生姓名
有C表 ,表里有
CNO INT --主键,课程名称编号
CNAME --课程名称
CTEACHER --授课老师姓名
有sc表,表里有
SNO --外键 学生编号
CNO --外键 ,课程编号
SCGRADE --学生成绩
问题是 :列出“1”号课程成绩比“2”号同学“1”号课程成绩高的所有学生编号
...全文
2909 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
htl258_Tony 2009-04-04
  • 打赏
  • 举报
回复
-->生成测试数据
if object_id('s') is not null
drop table s
GO
create table s
(
SNO INT, --主键,学生编号
SNAME VARCHAR(10)--学生姓名
)
go
insert s select 1 ,'a'
insert s select 2 ,'b'
insert s select 3 ,'c'
insert s select 4 ,'d'
insert s select 5 ,'e'

if object_id('c') is not null
drop table c
go
create table c
(
CNO INT, --主键,课程名称编号
CNAME varchar(10),--课程名称
CTEACHER varchar(10) --授课老师姓名
)
go
insert c select 1 ,'aa','q'
insert c select 2 ,'bb','w'
insert c select 3 ,'cc','e'

if object_id('sc') is not null
drop table sc
go
create table sc
(
SNO int, --外键 学生编号
CNO int, --外键 ,课程编号
SCGRADE int --学生成绩
)
go
insert sc select 1,1,90
insert sc select 1,2,70
insert sc select 1,3,75
insert sc select 2,1,80
insert sc select 2,2,83
insert sc select 2,3,87
insert sc select 3,1,80
insert sc select 3,2,78
insert sc select 3,3,88
insert sc select 4,1,82
insert sc select 4,2,84
insert sc select 4,3,95
insert sc select 5,1,89
insert sc select 5,2,75
insert sc select 5,3,86

select SNO from sc s where cno='1' and exists(select 1 from sc where cno='1' and sno='2' and scgrade<s.scgrade)
/*
SNO
-----------
1
4
5

(3 行受影响)
*/
楼主要求的是学生编号,这是学生编号(SNO)的。
htl258_Tony 2009-04-04
  • 打赏
  • 举报
回复
-->生成测试数据
if object_id('s') is not null
drop table s
GO
create table s
(
SNO INT, --主键,学生编号
SNAME VARCHAR(10)--学生姓名
)
go
insert s select 1 ,'a'
insert s select 2 ,'b'
insert s select 3 ,'c'
insert s select 4 ,'d'
insert s select 5 ,'e'

if object_id('c') is not null
drop table c
go
create table c
(
CNO INT, --主键,课程名称编号
CNAME varchar(10),--课程名称
CTEACHER varchar(10) --授课老师姓名
)
go
insert c select 1 ,'aa','q'
insert c select 2 ,'bb','w'
insert c select 3 ,'cc','e'

if object_id('sc') is not null
drop table sc
go
create table sc
(
SNO int, --外键 学生编号
CNO int, --外键 ,课程编号
SCGRADE int --学生成绩
)
go
insert sc select 1,1,90
insert sc select 1,2,70
insert sc select 1,3,75
insert sc select 2,1,80
insert sc select 2,2,83
insert sc select 2,3,87
insert sc select 3,1,80
insert sc select 3,2,78
insert sc select 3,3,88
insert sc select 4,1,82
insert sc select 4,2,84
insert sc select 4,3,95
insert sc select 5,1,89
insert sc select 5,2,75
insert sc select 5,3,86

select * from sc s where cno='1' and exists(select 1 from sc where cno='1' and sno='2' and scgrade<s.scgrade)
/*
SNO CNO SCGRADE
----------- ----------- -----------
1 1 90
4 1 82
5 1 89

(3 行受影响)
*/
lizzier 2009-04-04
  • 打赏
  • 举报
回复
select s.sno
from sc,c,s
where c.cno='1' and scgrade>(
select scgrade
from sc,c,s
where c.cno='1' and s.sno='2' )
  • 打赏
  • 举报
回复 1
select sno 
from sc
where cno = '1'
and scgrade >(
select SCGRADE from sc where sno = '2' and cno = '1')
-狙击手- 2009-04-04
  • 打赏
  • 举报
回复
[Quote=引用楼主 luoxiu128 的帖子:]
有S表,表里面有
SNO INT --主键,学生编号
SNAME --学生姓名
有C表 ,表里有
CNO INT --主键,课程名称编号
CNAME --课程名称
CTEACHER --授课老师姓名
有sc表,表里有
SNO --外键 学生编号
CNO --外键 ,课程编号
SCGRADE --学生成绩
问题是 :列出“1”号课程成绩比“2”号同学“1”号课程成绩高的所有学生编号
[/Quote]
select sno
from sc
where cno = '1'
and scgrade >(
select SCGRADE from sc where sno = '2' and cno = '1')
技术更新太快 2009-04-04
  • 打赏
  • 举报
回复
2号是学生编号啊,1号才是课程编号啊
我想起来了,这个不是我要的答案啊
只不过我觉得我那个想的太复杂了,
又没有比较简单的
select sc.sno from sc where sc.SCGRADE >select sc.SCGRADE FROM SC WHERE SC.CNO=(select c.cno from c where c.cname='1') and sc.sno=(select s.sno from s where s.sname='2')) and SC.CNO=(select c.cno from c where c.cname='1')
我是这样想的,不知道对不对
dawugui 2009-03-17
  • 打赏
  • 举报
回复
select m.sno from sc m , sc n where m.CNO = '1' and n.CNO = '2' and m.SCGRADE > n.SCGRADE

22,299

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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