day 35 (作业)

'''
create table class(
cid int auto_increment primary key,
caption char(4) not null default '尚未开班'
)charset utf8;

insert into class(caption) values('三年二班'), ('一年三班'), ('三年一班');

create table student(
sid int auto_increment primary key,
sname char(2) not null default 'sb',
gender enum('male','female') default 'female',
class_id int not null default 1,
constraint fk_st_cl foreign key (class_id) references class(cid)
)charset utf8;

insert into student(sname,gender,class_id) values
('钢蛋','female',1),
('铁锤','female',1),
('山炮','male',1),
('张三','female',1),
('李四','male',1),
('旺财','female',1),
('张青','male',1),
('张二','male',1),
('刘一','female',2),
('铁二','female',3),
('山鸡','male',3),
('张凯','female',2),
('李梅','male',2),
('旺仔','female',3),
('刘青','male',3),
('二狗','male',2);

create table teacher(
tid int auto_increment primary key,
tname char(2) not null default 'sb'
)charset utf8;

insert into teacher(tname) values
('叶平'),('波多'),('苍空'),('饭岛'),('小泽'),('李平'),('李一'),('李二');

create table course (
cid int auto_increment primary key,
cname char(3) not null default '神精学',
teacher_id int not null default 3,
constraint fk_co_te foreign key (teacher_id) references teacher(tid)
)charset utf8;

insert into course(cname,teacher_id) values
('生物课',1),('物理课',2),
('体育课',2),('英语课',3),
('日语课',4),('语文课',1),
('化学课',1),('地理课',5),
('心理课',4),('形体课',5),
('音乐课',3),('瑜伽课',2);

create table score(
sid int auto_increment primary key,
student_id int not null default 1,
course_id int not null default 5,
constraint fk_st_id foreign key (student_id) references student(sid),
constraint fk_co_id foreign key (course_id) references course(cid)
)charset utf8;

alter table score
add number int not null default 0;

insert into score(student_id,course_id,number) values
(1,2,90),(1,4,35),(2,9,65),(3,4,55),
(3,8,96),(5,6,78),(1,2,65),(16,10,25),
(4,12,100),(11,11,55),(6,6,99),(4,3,55),
(5,3,59),(13,9,60),(5,6,100),(6,10,100),
(6,1,24),(14,3,8),(1,5,6),(9,6,66),
(7,6,66),(15,2,69),(3,8,88),(8,7,78),
(8,8,88),(12,12,98),(9,1,65),(5,3,18),
(9,5,66),(10,6,64),(9,9,19),(1,3,96);

-- 1. 查询所有大于60分的学生的姓名和学号 (DISTINCT: 去重)

select sid,sname from student where sid in (select distinct student_id from score where number>60);

-- 2.查询每个老师教授的课程数量 和 老师信息

select teacher_id as id,tname,count(cid) as unm from
(select cid,teacher_id,tname from course left join teacher on teacher_id = teacher.tid) as ff
group by teacher_id;

-- 3. 查询学生的信息以及学生所在的班级信息

select sid,sname,caption from student left join class on class_id = class.cid;

-- 4、学生中男生的个数和女生的个数

select gender,count(sid) from student group by gender;

-- 5、获取所有学习'生物课'的学生的学号和成绩;姓名

select student.sid,sname,number from student right join
(select * from score where course_id = 1)as score
on student.sid = student_id;

-- 6、查询平均成绩大于60分的同学的学号和平均成绩;

select student_id as id,sname,avg_n from student right join
(select student_id,avg(number) as avg_n from score group by student_id having avg_n>60) as score
on student_id = sid;

-- 7、查询姓“李”的老师的个数;

select count(tid) as l_count from teacher where tname like '李%' ;

-- 8、查询课程成绩小于60分的同学的学号、姓名;

select sid,sname from student right join
(select distinct student_id from score where number<60) as score
on sid = student_id;

-- 9. 删除学习“叶平”老师课的SC表记录

delete from score where course_id in
(select cid from course left join teacher on teacher_id = tid where tname='叶平');

-- 10.查询各科成绩最高和最低的分:以如下形式显示:课程ID,最高分,最低分;

select cname,number from course right join
(select max(number) as number,course_id from score group by course_id) as score
on cid = course_id;

-- 11.查询每门课程被选修的学生数

select cname,num_st from course left join
(select count(sid) as num_st,course_id from score group by course_id) as score
on cid = course_id;

-- 12.查询姓“张”的学生名单;

select sid,sname from student where sname like '张%';

-- 13.查询每门课程的平均成绩,结果按平均成绩升序排列,平均成绩相同时,按课程号降序排列

select cid,cname,avg_n from course right join
(select course_id,avg(number) as avg_n from score group by course_id ) as score
on cid = course_id order by avg_n asc,cid desc;

-- 14.查询平均成绩大于85的所有学生的学号、姓名和平均成绩

select sid,sname,avg_n from student right join
(select student_id,avg(number) as avg_n from score group by student_id having avg_n>85) as score
on sid = student_id;

-- 15.查询课程编号为3且课程成绩在80分以上的学生的学号和姓名;

select sid,sname from student right join
(select student_id from score where course_id =3 and number>80) as score
on sid = student_id;

-- 16.查询各个课程及相应的选修人数

select cname,num_st from course left join
(select count(sid) as num_st,course_id from score group by course_id) as score
on cid = course_id;

-- 17.查询“4”课程分数小于60,按分数降序排列的同学学号

select sid,sname,number from student right join
(select student_id,number from score where course_id = 4 and number<60) as score
on sid = student_id order by number desc;

-- 18.删除学号为“2”的同学的“1”课程的成绩

delete from score where student_id=2 and course_id=1;

'''
'''

+-----+--------------+
| cid | caption |
+-----+--------------+
| 1 | 三年二班 |
| 2 | 一年三班 |
| 3 | 三年一班 |
+-----+--------------+

+-----+-----------+------------+
| cid | cname | teacher_id |
+-----+-----------+------------+
| 1 | 生物课 | 1 |
| 2 | 物理课 | 2 |
| 3 | 体育课 | 2 |
| 4 | 英语课 | 3 |
| 5 | 日语课 | 4 |
| 6 | 语文课 | 1 |
| 7 | 化学课 | 1 |
| 8 | 地理课 | 5 |
| 9 | 心理课 | 4 |
| 10 | 形体课 | 5 |
| 11 | 音乐课 | 3 |
| 12 | 瑜伽课 | 2 |
+-----+-----------+------------+
12 rows in set (0.00 sec)

+-----+--------+
| tid | tname |
+-----+--------+
| 1 | 叶平 |
| 2 | 波多 |
| 3 | 苍空 |
| 4 | 饭岛 |
| 5 | 小泽 |
+-----+--------+
5 rows in set (0.00 sec)

+-----+--------+--------+----------+
| sid | sname | gender | class_id |
+-----+--------+--------+----------+
| 1 | 钢蛋 | female | 1 |
| 2 | 铁锤 | female | 1 |
| 3 | 山炮 | male | 1 |
| 4 | 张三 | female | 1 |
| 5 | 李四 | male | 1 |
| 6 | 旺财 | female | 1 |
| 7 | 张青 | male | 1 |
| 8 | 张二 | male | 1 |
| 9 | 刘一 | female | 2 |
| 10 | 铁二 | female | 3 |
| 11 | 山鸡 | male | 3 |
| 12 | 张凯 | female | 2 |
| 13 | 李梅 | male | 2 |
| 14 | 旺仔 | female | 3 |
| 15 | 刘青 | male | 3 |
| 16 | 二狗 | male | 2 |
+-----+--------+--------+----------+
16 rows in set (0.00 sec)

+-----+------------+-----------+--------+
| sid | student_id | course_id | number |
+-----+------------+-----------+--------+
| 1 | 1 | 2 | 90 |
| 2 | 1 | 3 | 35 |
| 3 | 2 | 9 | 65 |
| 4 | 3 | 8 | 65 |
| 5 | 3 | 8 | 96 |
| 6 | 5 | 6 | 78 |
| 7 | 1 | 2 | 65 |
| 8 | 16 | 10 | 25 |
| 9 | 4 | 12 | 100 |
| 10 | 11 | 11 | 55 |
| 11 | 6 | 6 | 99 |
| 12 | 4 | 3 | 55 |
| 13 | 5 | 3 | 59 |
| 14 | 13 | 9 | 60 |
| 15 | 5 | 6 | 100 |
| 16 | 6 | 10 | 100 |
| 17 | 6 | 1 | 24 |
| 18 | 14 | 3 | 8 |
| 19 | 1 | 5 | 6 |
| 20 | 9 | 6 | 66 |
| 21 | 7 | 6 | 66 |
| 22 | 15 | 2 | 69 |
| 23 | 3 | 8 | 88 |
| 24 | 8 | 7 | 78 |
| 25 | 8 | 8 | 88 |
| 26 | 12 | 12 | 98 |
| 27 | 9 | 1 | 65 |
| 28 | 5 | 3 | 18 |
| 29 | 9 | 5 | 66 |
| 30 | 10 | 6 | 64 |
| 31 | 9 | 9 | 19 |
| 32 | 1 | 3 | 32 |
+-----+------------+-----------+--------+
32 rows in set (0.00 sec)

'''

猜你喜欢

转载自www.cnblogs.com/luocongyu/p/11769161.html