2、问题描述:
已知关系模式:
1、s (sno,sname) 学生关系。sno 为学号,sname 为姓名
2、c (cno,cname,cteacher) 课程关系
cno 为课程号,cname 为课程名,cteacher 为任课教师
3、sc(sno,cno,scgrade) 选课关系。scgrade 为成绩
要求实现如下5 个处理:
1.找出没有选修过“李明”老师讲授课程的所有学生姓名
2.列出有二门以上(含两门)不及格课程的学生姓名及其平均成绩
3.列出既学过“1”号课程,又学过“2”号课程的所有学生姓名
4.列出“1”号课成绩比“2”号同学该门课成绩高的所有学生的学号
5.列出“1”号课成绩比“2”号课成绩高的所有学生的学号及其“1”号课和
“2”号课的成绩
Oracle的问题
答案:2 悬赏:0 手机版
解决时间 2021-02-28 22:02
- 提问者网友:斑駁影
- 2021-02-28 17:32
最佳答案
- 五星知识达人网友:夜余生
- 2021-02-28 18:52
1.找出没有选修过“李明”老师讲授课程的所有学生姓名
--实现代码:
Select Sname As 学生姓名
From s
Where Not Exists (Select *
From c, Sc
Where c.Cno = Sc.Cno
And Cteacher = '李明'
And Sc.Sno = s.Sno);
Select Sname As 学生姓名
From s
Where Sno Not In (Select Sno
From c, Sc
Where c.Cno = Sc.Cno
And Cteacher = '李明');
2. 列出有二门以上(含两门)不及格课程的学生姓名及其平均成绩
--实现代码:
Select s.Sno As 学生学号, s.Sname As 学生姓名, Avg(Sc.Scgrade) As 平均成绩
From s, Sc
Where Sc.Sno = s.Sno
And Sc.Sno In (Select Sc.Sno
From Sc
Where Sc.Scgrade < 60
Group By Sc.Sno
Having Count(*) > 2)
Group By s.Sno, s.Sname;
3. 列出既学过“01”号课程,又学过“02”号课程的所有学生姓名
--实现代码:
select s.sno as 学生学号,s.sname as 学生姓名 from s where sno in(select sc.sno as 学生学号 from c,sc where c.cno=sc.cno and c.cno in('01','02') group by sno having count(distinct sc.cno)=2);
4. 列出“01”号课成绩比“02”号同学该门课成绩高的所有学生的学号
--实现代码:
select sc1.sno as 学生学号 from sc as sc1,c as c1,sc as sc2,c as c2
where sc1.cno=c1.cno and c1.cno='01' and sc2.cno=c2.cno and c2.cno='02'
and sc1.scgrade>sc2.scgrade group by sc1.sno;
5. 列出“01”号课成绩比“02”号课成绩高的所有学生的学号及其“01”号课和“02”号课的成绩
--实现代码:
select sc1.sno as 学生学号, sc1.scgrade as no1grade ,sc2.scgrade as no2grade
from sc as sc1,c as c1,sc as sc2,c as c2
where sc1.cno=c1.cno and c1.cno='01' and sc2.cno=c2.cno and c2.cno='02'
and sc1.scgrade>sc2.scgrade group by sc1.sno;
--实现代码:
Select Sname As 学生姓名
From s
Where Not Exists (Select *
From c, Sc
Where c.Cno = Sc.Cno
And Cteacher = '李明'
And Sc.Sno = s.Sno);
Select Sname As 学生姓名
From s
Where Sno Not In (Select Sno
From c, Sc
Where c.Cno = Sc.Cno
And Cteacher = '李明');
2. 列出有二门以上(含两门)不及格课程的学生姓名及其平均成绩
--实现代码:
Select s.Sno As 学生学号, s.Sname As 学生姓名, Avg(Sc.Scgrade) As 平均成绩
From s, Sc
Where Sc.Sno = s.Sno
And Sc.Sno In (Select Sc.Sno
From Sc
Where Sc.Scgrade < 60
Group By Sc.Sno
Having Count(*) > 2)
Group By s.Sno, s.Sname;
3. 列出既学过“01”号课程,又学过“02”号课程的所有学生姓名
--实现代码:
select s.sno as 学生学号,s.sname as 学生姓名 from s where sno in(select sc.sno as 学生学号 from c,sc where c.cno=sc.cno and c.cno in('01','02') group by sno having count(distinct sc.cno)=2);
4. 列出“01”号课成绩比“02”号同学该门课成绩高的所有学生的学号
--实现代码:
select sc1.sno as 学生学号 from sc as sc1,c as c1,sc as sc2,c as c2
where sc1.cno=c1.cno and c1.cno='01' and sc2.cno=c2.cno and c2.cno='02'
and sc1.scgrade>sc2.scgrade group by sc1.sno;
5. 列出“01”号课成绩比“02”号课成绩高的所有学生的学号及其“01”号课和“02”号课的成绩
--实现代码:
select sc1.sno as 学生学号, sc1.scgrade as no1grade ,sc2.scgrade as no2grade
from sc as sc1,c as c1,sc as sc2,c as c2
where sc1.cno=c1.cno and c1.cno='01' and sc2.cno=c2.cno and c2.cno='02'
and sc1.scgrade>sc2.scgrade group by sc1.sno;
全部回答
- 1楼网友:一叶十三刺
- 2021-02-28 19:01
create or replace function getnumfromstr(str in varchar2) return number
is
v_str varchar2(1000);
begin
if str is null or trim(str) = '' then
return 0;
else
v_str := regexp(str,'[:alpha:]',''); --这地方用了oracle的正则表达式,[:alpha:]可以匹配字符a-z和a-z
if v_str is null then
return 0;
else
return to_number(v_str);
end if;
end if;
exception
when others then
dbms_output.put_line(sqlcode||': 'sqlerrm);
end;
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯