SQL 2005 检索没有选修c1课程且选修课程数为两门的学生的姓名和平均成绩并按平均成绩降序排列。
S表:s#(学号),sn(学生姓名)C表:c#(课程号),cn(课程名称)SC表:s#(学号),c#(课程号),gr(成绩)T表:t#(教师号),tn(教师姓名),c#(...
S表:s#(学号),sn(学生姓名)
C表:c#(课程号),cn(课程名称)
SC表:s#(学号),c#(课程号),gr(成绩)
T表:t#(教师号),tn(教师姓名),c#(所讲课程号)
检索没有选修c1课程且选修课程数为两门的学生的姓名和平均成绩并按平均成绩降序排列。
Select sn as 姓名,avg(gr) as 平均成绩
From s,sc
where sn not in (select sn
from s,sc
where c#!='c1' and s.s#=sc.s#
group by sn
having count(*)=2)
group by sn
order by avg(gr) desc
上面是我自己写的查询语句,但有些问题,想不通要怎么修改,求助~~~~~~~ 展开
C表:c#(课程号),cn(课程名称)
SC表:s#(学号),c#(课程号),gr(成绩)
T表:t#(教师号),tn(教师姓名),c#(所讲课程号)
检索没有选修c1课程且选修课程数为两门的学生的姓名和平均成绩并按平均成绩降序排列。
Select sn as 姓名,avg(gr) as 平均成绩
From s,sc
where sn not in (select sn
from s,sc
where c#!='c1' and s.s#=sc.s#
group by sn
having count(*)=2)
group by sn
order by avg(gr) desc
上面是我自己写的查询语句,但有些问题,想不通要怎么修改,求助~~~~~~~ 展开
展开全部
Select a.sn as 姓名,avg(c.gr) as 平均成绩
From s a,c b,sc c
where a.s#=c.s#
and b.c#=c.c#
and b.cn!='c1'
group by a.sn
having count(*)=2
order by avg(c.gr) desc
From s a,c b,sc c
where a.s#=c.s#
and b.c#=c.c#
and b.cn!='c1'
group by a.sn
having count(*)=2
order by avg(c.gr) desc
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
引用真灵级存在的回答:
Select a.sn as 姓名,avg(c.gr) as 平均成绩
From s a,c b,sc c
where a.s#=c.s#
and b.c#=c.c#
and b.cn!='c1'
group by a.sn
having count(*)=2
order by avg(c.gr) desc
Select a.sn as 姓名,avg(c.gr) as 平均成绩
From s a,c b,sc c
where a.s#=c.s#
and b.c#=c.c#
and b.cn!='c1'
group by a.sn
having count(*)=2
order by avg(c.gr) desc
展开全部
最佳答案里的cn!='c1'应该是c#!='c1',但这样做也不符合,因为无法排除选了c1也同时选了另外2门课的情况。我的答案如下,因为老师说字段名最好不带符号,所以我把s#、c#、t#分别改为sno、cno和tno
select sn,AVG(gr) as '平均成绩'
from s,sc
where s.sno=sc.sno
and s.sno not in (select s.sno from s,sc
where s.sno=sc.sno and cno='c1')
group by sn
having (COUNT(sc.cno)=2)
order by avg(gr)
select sn,AVG(gr) as '平均成绩'
from s,sc
where s.sno=sc.sno
and s.sno not in (select s.sno from s,sc
where s.sno=sc.sno and cno='c1')
group by sn
having (COUNT(sc.cno)=2)
order by avg(gr)
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询