有3个表S(学生表),C(课程表),SC(学生选课表)S(SNO,SNAME)代表(学号,姓名)
C(CNO,CNAME,CTEACHER)代表(课号,课名,教师)SC(SNO,CNO,SCGRADE)代表(学号,课号,成绩)
问题:
1,找出没选过“黎明”老师的所有学生姓名。
2,列出2门以上(含2门)不及格学生姓名及平均成绩。
3,即学过1号课程又学过2号课所有学生的姓名。
创建表和导入表数据
create table s(
sno int(4) primary key auto_increment,
sname varchar(32)
);
insert into s(sname) values('zhangsan');
insert into s(sname) values('lisi');
insert into s(sname) values('wangwu');
insert into s(sname) values('zhaoliu');
create table c(
cno int(4) primary key auto_increment,
cname varchar(32),
cteacher varchar(32)
);
insert into c(cname,cteacher) values('Java','吴老师');
insert into c(cname,cteacher) values('C++','王老师');
insert into c(cname,cteacher) values('C##','张老师');
insert into c(cname,cteacher) values('MySQL','郭老师');
insert into c(cname,cteacher) values('Oracle','黎明');
create table sc(
sno int(4),
cno int(4),
scgrade double(3,1),
constraint sc_sno_cno_pk primary key(sno,cno),
constraint sc_sno_fk foreign key(sno) references s(sno),
constraint sc_cno_fk foreign key(cno) references c(cno)
);
insert into sc(sno,cno,scgrade) values(1,1,30);
insert into sc(sno,cno,scgrade) values(1,2,50);
insert into sc(sno,cno,scgrade) values(1,3,80);
insert into sc(sno,cno,scgrade) values(1,4,90);
insert into sc(sno,cno,scgrade) values(1,5,70);
insert into sc(sno,cno,scgrade) values(2,2,80);
insert into sc(sno,cno,scgrade) values(2,3,50);
insert into sc(sno,cno,scgrade) values(2,4,70);
insert into sc(sno,cno,scgrade) values(2,5,80);
insert into sc(sno,cno,scgrade) values(3,1,60);
insert into sc(sno,cno,scgrade) values(3,2,70);
insert into sc(sno,cno,scgrade) values(3,3,80);
insert into sc(sno,cno,scgrade) values(3,4,60);
insert into sc(sno,cno,scgrade) values(4,3,50);
insert into sc(sno,cno,scgrade) values(4,4,80);
1.找出没选过“黎明”老师的所有学生姓名。
先找出选过黎明老师的学生编号-> 黎明老师的授课的编号
select cno from c where cteacher = '黎明';
select sno from sc where cno =(select cno from c where cteacher = '黎明');
select * from s where sno not in(select sno from sc where cno =(select cno from c where cteacher = '黎明'));
+-----+---------+
| sno | sname |
+-----+---------+
| 3 | wangwu |
| 4 | zhaoliu |
+-----+---------+
2.列出2门以上(含2门)不及格学生姓名及平均成绩。
select
t1.sname,t2.avgscgrade
from
(select
sc.sno,s.sname,count(*) as studentNum
from
sc
join
s
on
sc.sno = s.sno
where
scgrade <60
group by
sc.sno,s.sname
having
studentNum >=2)t1
join
(select
sc.sno,avg(sc.scgrade) as avgscgrade
from
sc
group by
sc.sno)t2
on
t1.sno = t2.sno;
+----------+------------+
| sname | avgscgrade |
+----------+------------+
| zhangsan | 64.00000 |
+----------+------------+
3.即学过1号课程又学过2号课所有学生的姓名
select
s.sname
from
sc
join
s
on
sc.sno = s.sno
where
cno=1 and sc.sno in(select sno from sc where cno=2);
+----------+
| sname |
+----------+
| zhangsan |
| wangwu |
+----------+