sql的一个高级查询问题

complety 2011-10-14 08:57:14
create table student(
sno char(10) primary key, --学号
sname nvarchar(20), --姓名
ssex bit, --性别
birthdate smalldatetime, --出生日期
sdept varchar(10) --所属院系
)
create table course
( cno char(6) primary key, --课程号
cname nvarchar(20), --课程名
ccredit int, --学分
pcno char(6) --先行课课程号
)
create table sc
(
sno char(10), --学号
cno char(6), --课程号
grade int, --课程成绩
primary key (sno,cno)
这个是3张表
问题-查询各门课程的最高分的的学生情况,要求列出学号、姓名、课程号、课程名、该课程的最高分的成绩
怎么实现这个呢 我想了好长时间 不晓得如何实现
...全文
109 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
--小F-- 2011-10-14
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 qianjin036a 的回复:]
引用 2 楼 jzf2050 的回复:
SQL code


select a.sno,a.sname,b.cno,b.cname,c.grade
from student a,course b,sc c
where c.grade=
(
select max(grade)
from sc
group by cno
)


这样做效率可能要差点.
[/Quote]

差不多的 晴天大大可以自己去测试下
--小F-- 2011-10-14
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 complety 的回复:]
答案是没错,
但是我有点不理解这个1的作用是什么啊
[/Quote]
exists是表示存在 是个BOOL值 所以在后面子查询中判断存在不需要指定具体的字段 只要是一个存在的值就可以了
所以就写1 其实写2 写任何常量都可以
jingtuzhong 2011-10-14
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 jzf2050 的回复:]
SQL code

select a.sno,a.sname,b.cno,b.cname,c.grade
from student a,course b,sc c
where a.sno=c.sno and b.cno=c.cno and c.grade=
(
select max(grade)
from sc d
where d.cno=b.cno
)
[/Quote]
同上
complety 2011-10-14
  • 打赏
  • 举报
回复
select a.sno,a.sname,b.cno,b.cname,c.grade
from student a,course b,sc c
where c.grade=
(
select max(grade)
from sc
group by cno
)
这个是错的啊
我以前就是这样做的
-晴天 2011-10-14
  • 打赏
  • 举报
回复
这样理解:
--先是三表连接查询,这没什么复杂的,最正常的三表连接:
select a.sno,a.sname,c.cno,c.cname,b.grade
from student a inner join sc b on a.sno=b.sno
inner join course c on c.cno=b.cno
--条件:因为要找的是该课程的最高分,因此,条件是:
--不存在这样一种情况,即sc表中没有cno与当前连接查询中的那个cno相同,
--且成绩大于当前查询那条记录的成绩的
where not exists(select 1 from sc where cno=b.cno and grade>b.grade)

-晴天 2011-10-14
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 jzf2050 的回复:]
SQL code


select a.sno,a.sname,b.cno,b.cname,c.grade
from student a,course b,sc c
where c.grade=
(
select max(grade)
from sc
group by cno
)
[/Quote]

这样做效率可能要差点.
complety 2011-10-14
  • 打赏
  • 举报
回复
那个1可以换成任何数 ,
这个内连接倒是有点小复杂呢
complety 2011-10-14
  • 打赏
  • 举报
回复
2个方法都实现了,
图像是蜡笔小新的说的那个方法很容易接受
我再想想你的这个方法
Lemon2050 2011-10-14
  • 打赏
  • 举报
回复
1表示存在啊,其实你也可以select 2
Lemon2050 2011-10-14
  • 打赏
  • 举报
回复

select a.sno,a.sname,b.cno,b.cname,c.grade
from student a,course b,sc c
where a.sno=c.sno and b.cno=c.cno and c.grade=
(
select max(grade)
from sc d
where d.cno=b.cno
)
complety 2011-10-14
  • 打赏
  • 举报
回复
答案是没错,
但是我有点不理解这个1的作用是什么啊
Lemon2050 2011-10-14
  • 打赏
  • 举报
回复


select a.sno,a.sname,b.cno,b.cname,c.grade
from student a,course b,sc c
where c.grade=
(
select max(grade)
from sc
group by cno
)

-晴天 2011-10-14
  • 打赏
  • 举报
回复
select a.sno,a.sname,c.cno,c.cname,b.grade
from student a inner join sc b on a.sno=b.sno
inner join course c on c.cno=b.cno
where not exists(select 1 from sc where cno=b.cno and grade>b.grade)

27,579

社区成员

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

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