MySQL操作示例

"""
MySQL综合练习作业
"""

# 1、自行创建测试数据;
# 创建数据库
"""
create database school;  
use school;  
"""
# 创建表结构
"""
create table class(
cid int primary key auto_increment, 
caption char(10) not null,
grade_id int unsigned not null
)engine=InnoDB default charset=utf8;  # 班级表
  
create table student(
sid int primary key auto_increment, 
sname char(10) not null,
gender enum("male","female") not null default "male",
class_id int unsigned not null
)engine=InnoDB default charset=utf8;  # 学生表
   
create table teacher(
tid int primary key auto_increment, 
tname char(10) not null
)engine=InnoDB default charset=utf8;  # 教师表
  
create table course(
cid int primary key auto_increment, 
cname char(10) not null,
teacher_id int unsigned not null
)engine=InnoDB default charset=utf8;  # 课程表
   
create table score(
sid int primary key auto_increment, 
student_id int unsigned not null,
course_id int unsigned not null,
score int unsigned not null 
)engine=InnoDB default charset=utf8;  # 课程表
   
create table class_grade(
gid int primary key auto_increment, 
gname char(20) not null
)engine=InnoDB default charset=utf8;  # 年级表
  
create table teach2cls(
tcid int primary key auto_increment, 
tid int unsigned not null,
cid int unsigned not null
)engine=InnoDB default charset=utf8;  # 班级任职表

"""
# 插入记录
"""
insert into class(caption,grade_id) values 
("一年一班",1),
("二年一班",2),
("一年二班",1),
("二年二班",2),
("二年三班",2),
("三年一班",3),
("四年一班",4),
("四年二班",4);

insert into student(sname,gender,class_id) values 
("judy", "female", 1),
("张三", "male", 1),
("李四", "female", 1),
("李武", "male", 1),
("kitty", "female", 2),
("walter", "female", 2),
("doris", "male", 2),
("周公", "male", 2),
("胡图", "male", 2),
("李毅", "male", 2),
("李二", "female", 3),
("李三", "female", 3),
("李柳", "male", 3),
("张八", "male", 3),
("王二", "male", 3),
("王三", "male", 3),
("王八", "female", 3),
("张八", "male", 4),
("王二", "female", 4),
("王三", "male", 4);

insert into teacher(tname) values 
("王五"),
("李平"),
("李艳");


insert into course(cname,teacher_id) values 
("生物", 1),
("物理", 2),
("化学", 2),
("英语", 3),
("语文", 2);

insert into score(student_id, course_id,score) values 
(1,1,80),
(1,2,90),
(1,3,88),
(1,4,100),
(1,5,100),
(2,1,67),
(2,3,99),
(3,1,55),
(3,2,33),
(3,4,80),
(4,2,56),
(4,3,23),
(4,4,88),
(5,1,78),
(6,2,45),
(6,4,100),
(6,3,80),
(7,1,98),
(7,2,85),
(7,3,87),
(7,4,74),
(8,1,81),
(8,2,82),
(8,3,90),
(9,4,96),
(10,1,88),
(12,1,80);

insert into class_grade(gname) values 
("一年级"),
("二年级"),
("三年级"),
("四年级"),
("五年级");

insert into teach2cls(tid,cid) values 
(1,1),
(1,2),
(2,2),
(3,1),
(3,2),
(3,3);
"""

# 2、查询学生总人数;
"""
select count(sid) as count_stu from student;
"""

# 3、查询“生物”课程和“物理”课程成绩都及格的学生id和姓名;
"""
select sid,sname from student inner join 
(select t1.student_id,t1.biology_score,t2.physical_score from 
  (select student_id,score as biology_score from score where course_id = 
    (select cid from course where cname = "生物") ) as t1  inner join 
  (select student_id,score as physical_score from score where course_id =
    (select cid from course where cname = "物理"))  as t2
on t1.student_id = t2.student_id
having biology_score >= 60 and physical_score >= 60) as t3
on t3.student_id = student.sid;
"""

# 4、查询每个年级的班级数,取出班级数最多的前三个年级;
"""
select gname,t1.class_count from class_grade inner join 
(select grade_id,count(cid) as class_count from class 
group by grade_id 
order by class_count desc 
limit 3) as t1
on gid = t1.grade_id;
"""

# 5、查询平均成绩最高和最低的学生的id和姓名以及平均成绩;
"""
select sid,sname,avg_score from student inner join 
((select student_id,avg(score) as avg_score from score
group by student_id
order by avg_score desc 
limit 1) 
union 
(select student_id,avg(score) as avg_score from score
group by student_id
order by avg_score asc  
limit 1)) as t1
on sid = t1.student_id;

"""

# 6、查询每个年级的学生人数;
"""
select gname,stu_num from class_grade inner join 
(select cid,t1.stu_num,grade_id from class inner join 
(select class_id,count(sid) as stu_num from student
  group by class_id) as t1
on cid = class_id) as t2
on gid = grade_id;
"""

# 7、查询每位学生的学号,姓名,选课数,平均成绩;
"""
select sid,sname,course_num,avg_score from student inner join 
(select student_id,count(course_id) as course_num,avg(score) as avg_score from score
  group by student_id) as t1
on sid = student_id;
"""

# 8、查询学生编号为“2”的学生的姓名、该学生成绩最高的课程名、成绩最低的课程名及分数;
"""
select sname,student_id,cname,score from student innr join 
(select student_id,cname,score from course inner join 
((select * from score where student_id = 2
order by score desc 
limit 1)
union 
(select * from score where student_id = 2
order by score asc
limit 1)) as t1
on course_id = cid) as t2
on sid = student_id;

"""

# 9、查询姓“李”的老师的个数和所带班级数;
"""
select count(distinct t2.cid) as class_count,count(distinct t2.tid) as teach_class_count from
(select teach2cls.tid,teach2cls.cid from teach2cls inner join 
(select tid from teacher where tname like "李%") as t1
on t1.tid = teach2cls.tid) as t2;
"""

# 10、查询班级数小于5的年级id和年级名;
"""
select gid,gname from class_grade inner join 
(select grade_id,count(cid),group_concat(caption) from class 
  group by grade_id
  having count(cid) < 5) as t1
on gid = grade_id;
"""

# 11、查询班级信息,包括班级id、班级名称、年级、年级级别(12为低年级,34为中年级,56为高年级),示例结果如下;
"""
select cid,caption,gname,
case
    when gname in ("一年级","二年级") then "低年级"
    when gname in ("三年级","四年级") then "中年级" 
    when gname in ("五年级","六年级") then "高年级" 
end as "grade_level"
from class inner join 
class_grade 
on grade_id = gid;
"""

# 12、查询学过“李平”老师2门课以上的同学的学号、姓名;
"""
select sid,sname from student where sid in 
(select student_id from score where course_id in
(select cid from course where teacher_id = (select tid from teacher where tname = "李平"))
group by student_id
having count(course_id) > 2);
"""

# 13、查询教授课程超过2门的老师的id和姓名;
"""
select tid,tname from teacher where tid in 
(select teacher_id from course 
  group by teacher_id 
  having count(cid) > 2);
"""

# 14、查询学过编号“1”课程和编号“2”课程的同学的学号、姓名;
"""
select sid,sname from student inner join 
(select distinct student_id from score where course_id in (1,2)) as t1
on student_id = sid;
"""

# 15、查询没有带过高年级的老师id和姓名;
"""
select teacher.tid,tname from teacher inner join
(select distinct tid from teach2cls where cid not in (5,6))as t1
on teacher.tid = t1.tid;
"""

# 16、查询学过“张三”老师所教的所有课的同学的学号、姓名;
"""

select sid,sname from student where sid in 
(select student_id from score,course,teacher
  where score.course_id = course.cid and course.teacher_id = teacher.tid and teacher.tname = "李平"
  group by student_id
  having count(course_id) = (select count(cid) from course,teacher
    where course.teacher_id = teacher.tid and teacher.tname = "李平"));

"""

# 17、查询带过超过2个班级的老师的id和姓名;
"""
select teacher.tid,tname from teacher inner join (
select tid, count(cid) from teach2cls 
  group by tid
  having count(cid) > 2) as t1
on teacher.tid = t1.tid;
"""

# 18、查询课程编号“2”的成绩比课程编号“1”课程低的所有同学的学号、姓名;
"""
select sid,sname from student where sid in
  (select t3.student_id from 
    (select t1.student_id,t1.score2,t2.score1 from
      (select student_id,score as score2 from score where course_id = 2) as t1 inner join
        (select student_id,score as score1 from score where course_id = 1) as t2
      on t1.student_id = t2.student_id) as t3
    where t3.score2 < t3.score1 
  );
"""

# 19、查询所带班级数最多的老师id和姓名;
"""
select tid,tname from teacher where tid = 
  (select tid from teach2cls 
    group by tid
    order by count(cid) desc 
    limit 1
  );

"""

# 20、查询有课程成绩小于60分的同学的学号、姓名;
"""
select sid,sname from student 
  where sid in 
    (select distinct student_id from score 
      where score < 60
    );
  
"""

# 21、查询没有学全所有课的同学的学号、姓名;
"""
select sid,sname from student where sid not in 
  (select student_id from score 
    group by student_id
    having count(course_id) = (select count(cid) from course)
  );
"""

# 22、查询至少有一门课与学号为“1”的同学所学相同的同学的学号和姓名;
"""
select sid,sname from student where sid in (
  select student_id from score 
    where course_id in
      (select course_id from score where student_id = 1)
);
"""

# 23、查询至少学过学号为“1”同学所选课程中任意一门课的其他同学学号和姓名;
"""
select sid,sname from student where sid in (
  select student_id from score 
    where student_id != 1 and course_id in
      (select course_id from score where student_id = 1)
  );
"""

# 24、查询和“2”号同学学习的课程完全相同的其他同学的学号和姓名;
"""
select sid,sname from student where sid in (
  select student_id from score 
    where student_id != 2  
    group by student_id
    having group_concat(course_id) = 
        (select group_concat(course_id) from score 
            where student_id = 2  
            group by student_id
        )
  );
"""

# 25、删除学习“王五”老师课的score表记录;
"""
delete from score where course_id in (
  select cid from course where teacher_id = (
    select tid from teacher where tname = "王五"
  ));
"""

# 26、向score表中插入一些记录,这些记录要求符合以下条件:①没有上过编号“2”课程的同学学号;②插入“2”号课程的平均成绩;
"""
insert into score(student_id,course_id,score) 
select t1.sid,t1.cid,t2.avg_score from(
    (select sid,2 as cid from student where sid not in (
    select student_id from score where course_id = 2)) as t1,
    (select avg(score) as avg_score from score where course_id = 2) as t2
);
"""

# 27、按平均成绩从低到高显示所有学生的“语文”、“数学”、“英语”三门的课程成绩,按如下形式显示:
# 学生ID,语文,数学,英语,有效课程数,有效平均分;
"""

select t1.student_id,
(select score from score as t2 where t1.student_id = t2.student_id and t2.course_id = (select cid from course where cname = "语文")) as "语文",
(select score from score as t2 where t1.student_id = t2.student_id and t2.course_id = (select cid from course where cname = "数学")) as "数学",
(select score from score as t2 where t1.student_id = t2.student_id and t2.course_id = (select cid from course where cname = "英语")) as "英语",
count(t1.course_id) as "有效课程数",avg(t1.score) as "有效平均分" 
from score as t1 where course_id in (select cid from course where cname in ("语文","数学","英语"))
group by t1.student_id
order by avg(t1.score) asc;

"""

# 28、查询各科成绩最高和最低的分:以如下形式显示:课程ID,最高分,最低分;
"""
select course.cid,t1.max_sc,t1.min_sc from course 
  left join 
  (select course_id,max(score.score) as max_sc,min(score.score) as min_sc from score
    group by course_id) as t1
  on course.cid = t1.course_id;
"""

# 29、按各科平均成绩从低到高和及格率的百分数从高到低顺序;
"""
select course_id,avg(score) as avg_sc,
concat(sum(case when score >= 60 then 1 else 0 end)/count(*) * 100, "%")as percent
from score
group by course_id
order by avg_sc asc, percent desc;

"""

# 30、课程平均分从高到低显示(现实任课老师);
"""
select cid,cname,avg_sc,tname from 
  (select cid,cname,tname from course inner join teacher
  on teacher_id = tid) as t1 
  left join 
  (select course_id,avg(score) as avg_sc from score 
    group by course_id )as t2
  on cid = course_id
order by avg_sc desc;
"""

# 31、查询各科成绩前三名的记录(不考虑成绩并列情况)
"""
select course.cid,course.cname,t3.first_sc,t3.second_sc,t3.third_sc from 
(select course_id,
 (select score from score as t2 where t1.course_id = t2.course_id order by score desc limit 0, 1) as first_sc,
 (select score from score as t2 where t1.course_id = t2.course_id order by score desc limit 1, 1) as second_sc,
 (select score from score as t2 where t1.course_id = t2.course_id order by score desc limit 2, 1) as third_sc
 from score as t1
group by course_id) as t3
right join course
on course.cid = t3.course_id;

方法二:
SELECT
  student_id,
  score,
  course_id
FROM score r1
WHERE (SELECT count(1)
       FROM (SELECT DISTINCT
               score,
               course_id
             FROM score) r2
       WHERE r2.course_id = r1.course_id AND r2.score > r1.score) <= 2
ORDER BY course_id, score DESC;
"""

# 32、查询每门课程被选修的学生数;
"""
select course_id,count(student_id) as stu_num from score
  group by course_id;

"""

# 33、查询选修了2门以上课程的全部学生的学号和姓名;
"""
select sid,sname from student where sid in (
  select student_id from score
    group by student_id
    having count(course_id) > 2
);
"""

# 34、查询男生、女生的人数,按倒序排列;
"""
select gender,count(sid) as stu_num from student
  group by gender
  order by stu_num asc;
"""

# 35、查询姓“张”的学生名单;
"""
select * from student where sname like "张%";
"""

# 36、查询同名同姓学生名单,并统计同名人数;
"""
select sname,group_concat(sid) as sid,count(sid) as same_num from student
  group by sname
  having count(sid) >= 2; 
"""

# 37、查询每门课程的平均成绩,结果按平均成绩升序排列,平均成绩相同时,按课程号降序排列;
"""
select course_id,avg(score) as avg_score from score
  group by course_id
  order by avg(score) asc, course_id desc;
"""

# 38、查询课程名称为“数学”,且分数低于60的学生姓名和分数;物理
"""
select sname,score from student inner join 
(select student_id,score from score where score < 60 and course_id = (select cid from course where cname = "数学"))
as t1
on student.sid = t1.student_id;

"""

# 39、查询课程编号为“3”且课程成绩在80分以上的学生的学号和姓名;
"""
select sid,sname from student 
inner join (
select student_id,score from score where course_id = 3 
having score > 80
)as t1
on sid = student_id;

"""

# 40、求选修了课程的学生人数
"""
select count(t1.student_id) from(
select student_id from score
  group by student_id) as t1;
"""

# 41、查询选修“王五”老师所授课程的学生中,成绩最高和最低的学生姓名及其成绩;
"""
select sname,score from student 
inner join 
((select student_id,score from
(select student_id,score,course_id from score as t1 
  where course_id in (
    select cid from course 
      where teacher_id = (select tid from teacher where tname = "李平"))) as t1
  order by score desc
  limit 1)
union 
(select student_id,score from
(select student_id,score,course_id from score as t1 
  where course_id in (
    select cid from course 
      where teacher_id = (select tid from teacher where tname = "李平"))) as t1
  order by score asc
  limit 1)) as t2
on sid = student_id;

"""

# 42、查询各个课程及相应的选修人数;
"""
select cname,t1.stu_num from course 
inner join 
(select course_id,count(student_id) as stu_num from score
group by course_id) as t1
on course.cid = t1.course_id;
"""

# 43、查询不同课程但成绩相同的学生的学号、课程号、学生成绩;
"""
select t1.student_id,t1.course_id,t1.score from score as t1, score as t2
where t1.student_id != t2.student_id and t1.course_id != t2.course_id and t1.score = t2.score
order by t1.score desc;
"""

# 44、查询每门课程成绩最好的前两名学生id和姓名;
"""
select student.sid,student.sname,t4.course_id,t4.score from
    (select score.student_id,score.course_id,score.score from
        (select t1.course_id,
        (select score from score as t2 where t1.course_id = t2.course_id order by t2.score desc limit 0,1) as first_sc,
        (select score from score as t2 where t1.course_id = t2.course_id order by t2.score desc limit 1,1) as second_sc
        from score as t1 
        group by t1.course_id) as t3 
     inner join score 
     on score.course_id = t3.course_id
     where score.score in (t3.first_sc, t3.second_sc)) as t4
 inner join student
 on student.sid = t4.student_id
 order by t4.course_id desc;
"""

# 45、检索至少选修两门课程的学生学号;
"""
select student_id from score
group by student_id
having count(course_id) > 1;
"""

# 46、查询没有学生选修的课程的课程号和课程名;
"""
select cid,cname from course where cid not in(
select course_id from score
group by course_id);

"""

# 47、查询没带过任何班级的老师id和姓名;
"""
insert into teacher(tname) values ("张**");


select tid,tname from teacher where tid not in (
select tid from teach2cls
group by tid 
);
"""

# 48、查询有两门以上课程超过80分的学生id及其平均成绩;
"""
select student_id,avg(score) as avg_sc from score
where score > 80
group by student_id
having count(course_id) > 2;
"""

# 49、检索“3”课程分数小于60,按分数降序排列的同学学号;
"""
select student_id from score 
where course_id = 3 and score < 60
order by score desc;

"""

# 50、删除编号为“2”的同学的“1”课程的成绩;
"""
insert into score(student_id,course_id,score) values (2,1,88);


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

# 51、查询同时选修了物理课和生物课的学生id和姓名;
"""
select sid,sname from student where sid in (
    select t1.student_id from score as t1
    where t1.course_id in (select cid from course where cname in ("物理","生物"))
    group by t1.student_id
    having  count(t1.course_id) = 2);
"""
View Code
原文地址:https://www.cnblogs.com/yanlin-10/p/9343590.html