赞
踩
现在存在如下表关系
班级表:class | |
cid | caption |
1 | 三年二班 |
2 | 一年三班 |
3 | 三年一班 |
学生表:student | |||
Sid | Sname | Gender | Class_id |
1 | 钢蛋 | 女 | 1 |
2 | 铁锤 | 女 | 1 |
3 | 山炮 | 男 | 2 |
教师表:teacher | |
tid | tname |
1 | 叶平 |
2 | 李大圣 |
3 | 叶问 |
课程表:course | ||
Cid | Cname | Teacher_id |
1 | 生物 | 1 |
2 | 体育 | 1 |
3 | 物理 | 2 |
成绩表:score | |||
Sid | Student_id | Corse_id | number |
1 | 1 | 1 | 60 |
2 | 1 | 2 | 59 |
3 | 2 | 2 | 100 |
select sc1.Student_id 学号 , sc1.Corse_id 生物, sc1.number,sc2.Corse_id 体育,sc2.number from score sc1 left join score sc2 on sc1.Student_id=sc2.Student_id where sc1.Corse_id=(select cid from course where cname='生物') and sc2.Corse_id=(select cid from course where cname='体育') having sc1.number>sc2.number;
select Student_id, avg(number) from score group by Student_id;
select s.sid,s.sname,count(sc.corse_id) 选课数,sum(sc.number) 总成绩 from student s, score sc where s.sid=sc.Student_id group by Student_id;
select count(*) from teacher where tname like'%李%';
select sid,sname from student where sid not in (select distinct Student_id from score where Corse_id in (select cid from course where tid=(select tid from teacher where tname='叶平')));
select sid,sname from student where sid in (select distinct sc1.Student_id from score sc1, score sc2 where sc1.Corse_id=1 and sc2.Corse_id=2);
select sid,sname from student where sid in (select distinct Student_id from score where Corse_id in (select cid from course where tid=(select tid from teacher where tname='叶平')));
select sid,sname from student where sid in (select sc1.Student_id from score sc1, score sc2 where sc1.Student_id=sc2.Student_id and sc1.Corse_id=1 and sc2.Corse_id=2 and sc1.number>sc2.number);
select sid,sname from student where sid in (select Student_id from score where number<60);
select sid,sname from student where sid not in (select Student_id from score group by Student_id having count(Corse_id)=3);
select sid, sname from student where sid in (select distinct Student_id from score where Corse_id in (select Corse_id from score where Student_id=1))
select sid, sname from student where sid in (select distinct Student_id from score where Corse_id in (select Corse_id from score where Student_id=1) having Student_id<>1);
select sid,sname from student where sid in (select s.Student_id from (select sc1.Student_id from score sc1 join score sc2 on sc1.Corse_id=sc2.Corse_id where sc1.Student_id<>2 and sc2.Student_id=2) s group by s.Student_id having count(*)=(select count(*) from score where Student_id=2));
delete from score where Corse_id=(select cid from course where tid=(select tid from teacher where tname='叶问'));
删除前:
删除后:
insert into score (Student_id,Corse_id,number) select sid, 2, (select avg(number) from score where Corse_id=2) from student where sid not in (select Student_id from score where Corse_id=2);
select sc1.Student_id,sc1.number 生物,sc2.number 体育,sc3.number 物理,(sc1.number+sc2.number+sc3.number)/3 平均成绩 from score sc1, score sc2, score sc3 where sc1.Student_id=sc2.Student_id and sc2.Student_id =sc3.Student_id and sc1.Corse_id=1 and sc2.Corse_id=2 and sc3.Corse_id=3 group by sc1. Student_id order by 平均成绩 asc;
select Corse_id,max(number),min(number) from score group by Corse_id;
select Corse_id,avg(number) 平均成绩, 100*sum(case when (number)>=60 then 1 else 0 end)/count(*) 及格率 from score group by Corse_id order by 平均成绩 asc, 及格率 desc;
select sid,sname from student where sid in (select Student_id from score group by Student_id having count(*)=1);
所有操作完成后的表:
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。