• 作业35


    create table teacher(
    	tid int auto_increment primary key,
    	tname varchar(32) not null default ''
    )charset=utf8;
    
    insert into teacher(tname) values
         ('波多'),
         ('苍井'),
         ('饭岛');
    
    
    create table class(
        cid int auto_increment primary key,
        caption varchar(32) not null default ''
         )charset=utf8;
    insert into class(tname) values
         ('三年级二班'),
         ('一年级三班'), 
         ('三年级一班');
    	 
    	 
        
        
    create table student (
        sid int auto_increment primary key,
        sname  varchar(32) not null default '',
        gender enum('男','女'),
        class_id int not null default,
        constraint fk_class_student foreign key (class_id) references class(cid)
        )charset=utf8;
    
    insert into student(sname,gender,class_id) values
    ('钢蛋','女',1),('铁锤','女',1),('山炮','男',2);
    
        
    create table course(
        cid int auto_increment primary key,
        cname varchar(32) not null default '',
        teacher_id int not null default 1,
        constraint fk_teacher_course foreign key (teacher_id) referances teacher(tid)
         )charset = utf8;
    
    insert into course (cname,teach_id) values
    ('生物',1),('体育',1),('物理',2);
    
    create table score(
        sid int auto_increment primary key,
        student_id int not null dafault 1,
        course_id int not null dafault 1,
        number int int not null dafault '',
        constraint fk_student_score foreign key (student_id) references suudent(sid)
        constraint fk_course_score foreign key (course_id) reference course(cid)
        )charset = utf-8
    
    insert into score (student_id,course_id,number) values
    (1,1,60),(1,2,59),(2,2,100);
    
    1.查询所有大于60分的学生的姓名和学号 (DISTINCT: 去重)
    select distinct student.sid, sname from score join student on student_id = student.sid where score.number > 60;
    
    2.查询每个老师教授的课程数量 和 老师信息
    select teacher.tid, teacher.tname, count(cid) as course_num from teacher join course on teacher.tid = course.teach_id group by teacher.tid, teacher.tname;
    
    3. 查询学生的信息以及学生所在的班级信息
    select * from student join class  on student.class_id = class.cid;
    
    4、学生中男生的个数和女生的个数
    select gender, count(*) as num from student group by gender;
    
    5、获取所有学习'生物'的学生的学号和成绩;姓名
    select student.sid, student.sname, score.number from student join score on student.sid = score.student_id join course on score.course_id = course.cid where course.cname = '生物';
    
    6、查询平均成绩大于60分的同学的学号和平均成绩;
    select student.sid, avg(number) as avg_number from student join score on student.sid = score.student_id group by student.sid having avg_number > 60;
    
    7、查询姓“李”的老师的个数;
    select count(tname) from teacher where tname like '李%';
    
    8、查询课程成绩小于60分的同学的学号、姓名;
    select student.sid, sname from score join student on student_id = student.sid where score.number < 60;
    
    9. 删除学习“叶平”老师课的SC表记录
    delete from student where student.sid in (select score.student_id from course, teacher, score where course.teach_id = teacher.tid and course.cid = score.course_id and teacher.tname = '叶平');
    
    10.查询各科成绩最高和最低的分:以如下形式显示:课程ID,最高分,最低分;
    select course.cid, max(score.number) as max_num, min(score.number) as min_num from course join score on course.cid = score.course_id group by course.cid;
    
    11.查询每门课程被选修的学生数
    select course.cname,count(sname) as choose_num from course join score on course.cid = score.course_id join student on score.student_id = student.sid group by course.cname;
    
    12.查询姓“张”的学生名单;
    select * from student where sname like '张%'
    
    13.查询每门课程的平均成绩,结果按平均成绩升序排列,平均成绩相同时,按课程号降序排列
    select course.cid, course.cname, avg(score.number) as avg_score_number from course join score on course.cid = score.course_id group by course.cid, course.cname order by avg_score_number, course.cid desc;
    
    14.查询平均成绩大于85的所有学生的学号、姓名和平均成绩
    select student.sid, student.sname, avg(score.number) as avg_score_number from student join score on student.sid = score.student_id group by student.sid, student.sname having avg_score_number > 85;
    
    15.查询课程编号为3且课程成绩在80分以上的学生的学号和姓名;
    select student.sid, student.sname from student join score on student.sid = score.student_id where score.course_id = 3 and score.number >= 80;
    
    16.查询各个课程及相应的选修人数
    select course.cname, count(student_id) as student_num from course, score where course.cid = score.course_id group by course.cname
    
    17.查询“4”课程分数小于60,按分数降序排列的同学学号
    select student_id from score where course_id = 4 and number < 60 order by number desc;
    
    18.删除学号为“2”的同学的“1”课程的成绩
    delete from score where student_id = 2 and course_id = 1
    
  • 相关阅读:
    Elkstack2.0部署
    ZABBIX自定义用户KEY与参数USERPARAMETERS监控脚本输出
    elasticsearch 管理工具
    指定时间开启、结束定时器
    异步的作用
    快速生成几百万条测试数据
    查看查询索引
    蠕虫复制
    导出数据库
    header
  • 原文地址:https://www.cnblogs.com/gfhh/p/11768557.html
Copyright © 2020-2023  润新知