冰天雪地祼跪求答!!!!!!

todn 2008-04-09 05:23:08
老师给的题目如下,请高手们指点。
/*************************************************
问题描述:
已知关系模式:
学员表student (学号std_no,姓名std_name,性别sex,班号class_no)
1 张三 男 1001
2 李四 男 1001
3 王五 女 1002
4 陈六 男 1002
课程表course (课程号course_no,课程名course_name,授课老师teacher)
1001 JAVA 李明
1002 SQL 杨芳
1003 C# 刘宏
考试表score (学号std_no,课程号course_no,成绩score)
1 1001 90
1 1002 100
1 1003 80
2 1001 80
2 1002 40
2 1003 56
3 1001 70
3 1002 50

**************************************************/
--创建数据库
if exists(select name from sys.databases where name='accp')
drop table accp
create database accp
on primary
(
name = 'accp_data.mdf',
filename = 'D:\temp\DataBase\ch3\accp_data.mdf',
size = 5MB
)
log on
(
name = 'accp_log.ldf',
filename = 'D:\temp\DataBase\ch3\accp_log.ldf',
size = 3MB
)
go
--创建学员表student并插入数据
if exists(select name from sys.objects where name = 'student')
drop table student
create table student
(
std_no int not null identity(1,1) primary key,
std_name varchar(10) not null,
sex bit not null,
class_no int not null
)
go
insert into student
select '张三',0,1001 union
select '李四',0,1001 union
select '王五',1,1002 union
select '陈六',0,1002
go
select * from student
--创建课程表course并插入数据
if exists(select name from sys.objects where name = 'course')
drop table course
create table course
(
course_no int not null primary key,
course_name varchar(20) not null,
teacher varchar(10) not null
)
go
insert into course
select 1001,'JAVA','李明' union
select 1002,'SQL','杨芳' union
select 1003,'C#','刘宏'
go
select * from course
--创建考试表score并插入数据
if exists(select name from sys.objects where name = 'score')
drop table score
create table score
(
std_no int foreign key references student(std_no),
course_no int foreign key references course(course_no),
mark float
)
go
insert into score
select 1,1001,90 union
select 1,1002,100 union
select 1,1003,80 union
select 2,1001,80 union
select 2,1002,40 union
select 2,1003,56 union
select 3,1001,70 union
select 3,1002,50
go
select * from score
--1.查询有参加课程名称为'JAVA'考试的学员学号和姓名

--2.查询没有参加课程编号为1003考试的学员姓名

--3.查询参加了全部课程考试的学员姓名

--4. 查询没有考试的学员人数

--5. 找出没有参加"李明"老师讲授课程考试的所有学生姓名

--6. 列出有二门以上(含两门)不及格课程的学生姓名及其平均成绩

--7. 列出既学过1001号课程,又学过1002号课程的所有学生姓名

--8. 列出课程1001成绩比1002成绩高的所有学生的学号

--9. 列出课程1001成绩比1002成绩高的所有学生的学号及其1001和1002的成绩
...全文
142 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
青锋-SS 2008-04-11
  • 打赏
  • 举报
回复
哈哈,既然问题解决了,怎么不见结账呢!?
zccmy22 2008-04-11
  • 打赏
  • 举报
回复
不要这么说,在学校时就不信你就会,就这么牛,对小孩子要有耐心。小兄弟加油。啊,下次你记得这几个人,一定要超过他们,下次在他们的贴子上,丢块砖。。呵呵,我是不是像个小孩了。呵。
-狙击手- 2008-04-11
  • 打赏
  • 举报
回复

utpcb 2008-04-11
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 sweetweiwei 的回复:]
--1.查询有参加课程名称为'JAVA'考试的学员学号和姓名
select a.std_no,
a.std_name
from student a
inner join score b on a.std_no = b.std_no
inner join course c on c.course_no = b.course_no
where c.course_no = 1001

--2.查询没有参加课程编号为1003考试的学员姓名
select std_name
from student
where std_name not in
(select a.std_name
from student a
inner join score …
[/Quote]

太简单了好吧
Ray_Zhang 2008-04-11
  • 打赏
  • 举报
回复
5楼好人那
正宗老冉 2008-04-10
  • 打赏
  • 举报
回复
不建议直接给学生答案。
chenxin2835 2008-04-10
  • 打赏
  • 举报
回复
--答:
--1.查询有参加课程名称为'JAVA'考试的学员学号和姓名

select c.course_name,b.std_no,b.std_name
from score a
left join student b on a.std_no=b.std_no
left join course c on a.course_no=c.course_no
where c.course_name='JAVA'

--2.查询没有参加课程编号为1003考试的学员姓名

select std_name
from student
where std_name not in
(
select b.std_name
from score a
left join student b on a.std_no=b.std_no
left join course c on a.course_no=c.course_no
where c.course_no='1003'
)

--3.查询参加了全部课程考试的学员姓名

select std_name
from
(select std_no,count(std_no) as scnt from score group by std_no) a join
(select count(1) as ccnt from course) b on scnt=ccnt join
student c on a.std_no=c.std_no

/*函数实现
alter function dbo.fun_q(@std_no int)
returns varchar(10)
as
begin
declare @c1 int,@c2 int,@str varchar(10)

select @c1=count(1)
from course

select @c2=count(std_no)
from score
where std_no=@std_no

if @c1=@c2
select @str=std_name from student where std_no=@std_no
return (@str)
end
go

select a as [name]
from
(
select dbo.fun_q(std_no)as a from student
) as a
where a is not null
*/
--4. 查询没有考试的学员人数
select count(1)
from student
where std_no not in
(
select std_no from score
)

--5. 找出没有参加"李明"老师讲授课程考试的所有学生姓名
select std_name from student where std_name not in
(
select b.std_name
from score a
left join student b on a.std_no=b.std_no
left join course c on a.course_no=c.course_no
where c.teacher='李明'
)

--6. 列出有二门以上(含两门)不及格课程的学生姓名及其平均成绩


select std_name,avg(mark) as avgmark
from score a left join student b on a.std_no=b.std_no
where b.std_name in
(
select case when count(a.std_no)>=2 then std_name end as std_name
from score a left join student b on a.std_no=b.std_no
where a.mark<60
group by a.std_no,std_name
)
group by std_name

--7. 列出既学过1001号课程,又学过1002号课程的所有学生姓名
select std_name
from score a left join student b on a.std_no=b.std_no
where a.std_no in
(
select std_no
from score
where course_no='1001'
)
and course_no='1002'

--8. 列出课程1001成绩比1002成绩高的所有学生的学号
--9. 列出课程1001成绩比1002成绩高的所有学生的学号及其1001和1002的成绩
select d.* from
(
select case when a.mark>b.mark then a.std_no end as sno
from
(select * from score where course_no='1001') a join
(select * from score where course_no='1002') b on a.std_no=b.std_no
) as c join score d on c.sno=d.std_no
where sno is not null and course_no in('1001','1002')



答应过今天给你答案的,要下班了,就放出来了
wzy_love_sly 2008-04-09
  • 打赏
  • 举报
回复
顶5楼
-晴天 2008-04-09
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 sweetweiwei 的回复:]
--1.查询有参加课程名称为'JAVA'考试的学员学号和姓名
select a.std_no,
a.std_name
from student a
inner join score b on a.std_no = b.std_no
inner join course c on c.course_no = b.course_no
where c.course_no = 1001

--2.查询没有参加课程编号为1003考试的学员姓名
select std_name
from student
where std_name not in
(select a.std_name
from student a
inner join score …
[/Quote]

老师把答案贴出来了!
sweetweiwei 2008-04-09
  • 打赏
  • 举报
回复
--1.查询有参加课程名称为'JAVA'考试的学员学号和姓名
select a.std_no,
a.std_name
from student a
inner join score b on a.std_no = b.std_no
inner join course c on c.course_no = b.course_no
where c.course_no = 1001

--2.查询没有参加课程编号为1003考试的学员姓名
select std_name
from student
where std_name not in
(select a.std_name
from student a
inner join score b on a.std_no = b.std_no
inner join course c on c.course_no = b.course_no
where c.course_no = 1003)

--3.查询参加了全部课程考试的学员姓名
select std_name
from student
where std_no in
(select std_no from(
select std_no,
sum(case when course_no = 1001 or course_no= 1002 or course_no = 1003 then 1 else 0 end) as 'count'
from score
group by std_no
having count(*) = 3) c)

--4. 查询没有考试的学员人数???

--5. 找出没有参加"李明"老师讲授课程考试的所有学生姓名
select std_name
from student
where std_no not in
(select a.std_no
from student a
inner join score b on a.std_no = b.std_no
inner join course c on c.course_no = b.course_no
where c.course_no = 1001)

--6. 列出有二门以上(含两门)不及格课程的学生姓名及其平均成绩
select a.std_name,
b.PScore
from student a inner join
(select std_no,
sum(mark)/count(*) as 'PScore'
from score
where mark <= 60
group by std_no
having count(*) >= 2) b on a.std_no = b.std_no

--7. 列出既学过1001号课程,又学过1002号课程的所有学生姓名
select a.std_name
from student a
inner join
(select std_no,
sum(case when course_no = 1001 then 1 when course_no = 1002 then 1 else 0 end) as 'count'
from score
group by std_no) b on a.std_no = b.std_no

--8. 列出课程1001成绩比1002成绩高的所有学生的学号
select a.std_no
from
(select *
from score
where course_no = 1001) a
inner join (select *
from score
where course_no = 1002) b on a.std_no = b.std_no
where a.mark > b.mark

--9. 列出课程1001成绩比1002成绩高的所有学生的学号及其1001和1002的成绩
select a.std_no,
a.mark as '1001 mark',
b.mark as '1002 mark'
from
(select *
from score
where course_no = 1001) a
inner join (select *
from score
where course_no = 1002) b on a.std_no = b.std_no
where a.mark > b.mark
paulmake 2008-04-09
  • 打赏
  • 举报
回复
太长了,哈哈,老师问的自己下劲想,和同学讨论
正宗老冉 2008-04-09
  • 打赏
  • 举报
回复
老师给的题都拿出来问?
chenxin2835 2008-04-09
  • 打赏
  • 举报
回复
搁明儿再答你,自个想想,都是基础中的基础,这个不搞好以后就不要碰数据库这玩意儿了
青锋-SS 2008-04-09
  • 打赏
  • 举报
回复
现在的学生,真没法说.

22,210

社区成员

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

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