返回信息流学生成绩表:
Transcript(表名)
StudentId(学号) CourseCode(课程号) Score(分数)
从中查询:课程C01的成绩高于课程C02的所有学生的学号
请教查询语句怎么写
这是一条镜像帖。来源:北邮人论坛 / database / #2913同步于 2009/1/6
该镜像源已超过 30 天没有更新,可能在源站已被删除。
Database机器人发帖
请教一个查询
huhaoran005
2009/1/6镜像同步4 回复
订阅后,新回复会通过你的通知中心匿名送达。
4 条回复
select distinct StudentId from Transcript T
where(select Score from Transcript T1 where T1.CourseCode='C01' and T1.StudentId=T.StudentId)
>
(select Score from Transcript T2 where T2.CourseCode='C02' and T2.StudentId=T.StudentId)
select distinct studentid from
(select score from transcript t1 where t1.CourseCode = 'C01') tt1,
(select score from transcript t1 where t1.CourseCode = 'C02') tt2
where tt1.score>tt3.score
先从子查询做起:
select distinct t1.studentid
from
(select studentid,score sc1 from transcript t1 where t1.CourseCode = 'C01' ) t1,
(select studentid,score sc2 from transcript t1 where t1.CourseCode = 'C02' ) t2
where t1.studentid=t2.studentid
and t1.sc1 > t2.sc2
解释下吧
第一个子查询是查出所有学生的C01分数
表格内容举例如下
studentid score
1 90
2 80
第二个子查询是查出所有学生的C02分数
表格内容举例如下
studentid score
1 70
2 90
两个表以studentid 关联
得到关联结果如下
studentid sc1 studentid sc2
1 90 1 70
2 80 2 90
然后选择想要的结果查询出来就ok了
--除3楼回答外,可以试试exists:
select t.studentid from Transcript t
where t.coursecode='C01'
and exists(select 1 from Transcript b
where b.studentid=t.studentid
and b.coursecode='C02'
and b.score<t.score ) ;