• MySQL 数据查询小练习


    作业

    # 创建班级表
    create table class (
    cls_id int auto_increment primary key,
    cls_name varchar(10) not null default ''
    );
    
    insert into class (cls_name) values ('三年二班'), ('一年三班'), ('二年一班');
    
    # 创建老师表
    create table teacher (
    t_id int auto_increment primary key,
    t_name varchar(10) not null default ''
    );
    
    insert into teacher (t_name) values ('罗老师'), ('厨子'), ('乔帮主');
    
    #创建学生表
    create table student (
    s_id int auto_increment primary key,
    s_name varchar(10) not null default '',
    gender enum('男', '女') not null default '男',
    class_id int not null default 1,
    constraint fk_std_cls foreign key (class_id) references class(cls_id)
    );
    
    insert into student (s_name, gender, class_id ) values ('雷军', '男', 1), ('余大嘴', '男', 2), ('黄章', '女', 3);
    
    # 创建课程表
    create table course (
    c_id int auto_increment primary key,
    c_name varchar(10) not null default '',
    teacher_id int not null default 1,
    constraint fk_course_teacher foreign key (teacher_id ) references teacher(t_id)
    );
    
    insert into course (c_name, teacher_id ) values ('Python', 3), ('C', 1), ('Go',2);
    
    
    # 创建成绩表
    create table score (
    scr_id int auto_increment primary key,
    student_id int not null default 1,
    course_id int not null default 1,
    scr_num int not null default 0,
    constraint fk_scr_std foreign key (student_id ) references student(s_id),
    constraint fk_scr_crs foreign key (course_id ) references course(c_id)
    );
    
    insert into score (student_id, course_id, scr_num) values (1, 3, 80), (2, 1, 70), (1, 2, 60);
    
    # 班级
    +--------+----------+
    | cls_id | cls_name |
    +--------+----------+
    |      1 | 三年二班 |
    |      2 | 一年三班 |
    |      3 | 二年一班 |
    +--------+----------+
    
    # 老师
    +------+--------+
    | t_id | t_name |
    +------+--------+
    |    1 | 罗老师 |
    |    2 | 厨子   |
    |    3 | 乔帮主 |
    +------+--------+
    
    # 学生
    +------+--------+--------+----------+
    | s_id | s_name | gender | class_id |
    +------+--------+--------+----------+
    |    1 | 雷军   | 男     |        1 |
    |    2 | 余大嘴 | 男     |        2 |
    |    3 | 黄章   | 女     |        3 |
    +------+--------+--------+----------+
    
    # 课程
    +------+--------+------------+
    | c_id | c_name | teacher_id |
    +------+--------+------------+
    |    1 | Python |          3 |
    |    2 | C      |          1 |
    |    3 | Go     |          2 |
    +------+--------+------------+
    
    # 分数
    +--------+------------+-----------+---------+
    | scr_id | student_id | course_id | scr_num |
    +--------+------------+-----------+---------+
    |      1 |          1 |         3 |      80 |
    |      2 |          2 |         1 |      70 |
    |      3 |          1 |         2 |      60 |
    +--------+------------+-----------+---------+
    
    • 查询所有大于60分的学生的姓名和学号 (DISTINCT: 去重)
    mysql> select distinct s_name, student_id  from score left join student on student_id=student.s_id where scr_num > 60;
    +--------+------------+
    | s_name | student_id |
    +--------+------------+
    | 雷军   |          1 |
    | 余大嘴 |          2 |
    +--------+------------+
    2 rows in set (0.00 sec)
    
    • 查询每个老师教授的课程数量和老师信息
    mysql> select t_name, count(t_name) as crs_count from course left join teacher on teacher_id=teacher.t_id group by t_name;
    +--------+-----------+
    | t_name | crs_count |
    +--------+-----------+
    | 乔帮主 |         1 |
    | 厨子   |         1 |
    | 罗老师 |         1 |
    +--------+-----------+
    3 rows in set (0.00 sec)
    
    • 查询学生的信息以及学生所在的班级信息
    mysql> select * from student left join class on class_id=class.cls_id;
    +------+--------+--------+----------+--------+----------+
    | s_id | s_name | gender | class_id | cls_id | cls_name |
    +------+--------+--------+----------+--------+----------+
    |    1 | 雷军   | 男     |        1 |      1 | 三年二班 |
    |    2 | 余大嘴 | 男     |        2 |      2 | 一年三班 |
    |    3 | 黄章   | 女     |        3 |      3 | 二年一班 |
    +------+--------+--------+----------+--------+----------+
    3 rows in set (0.00 sec)
    
    • 学生中男生的个数和女生的个数
    
    mysql> select gender, count(gender) from student group by gender;
    +--------+---------------+
    | gender | count(gender) |
    +--------+---------------+
    | 男     |             2 |
    | 女     |             1 |
    +--------+---------------+
    2 rows in set (0.00 sec)
    
    • 获取所有学习 "Python" 的学生的学号, 成绩和姓名
    
    mysql> select c_name, s_id, s_name, scr_num from score left join student on student_id=student.s_id left join course on course_id=course.c_id where c_name='Python';
    +--------+------+--------+---------+
    | c_name | s_id | s_name | scr_num |
    +--------+------+--------+---------+
    | Python |    2 | 余大嘴 |      70 |
    +--------+------+--------+---------+
    1 row in set (0.00 sec)
    
    
    • 查询平均成绩大于60分的同学的信息和平均成绩
    mysql> select s_id, s_name, avg(scr_num) from score left join student on student_id=student.s_id group by s_name having avg(scr_num) > 60;
    +------+--------+--------------+
    | s_id | s_name | avg(scr_num) |
    +------+--------+--------------+
    |    2 | 余大嘴 |      70.0000 |
    |    1 | 雷军   |      70.0000 |
    +------+--------+--------------+
    2 rows in set (0.00 sec)
    
    • 查询姓“罗”的老师的个数;
    mysql> select t_name, count(t_name) from teacher group by t_name having t_name like '罗%';
    +--------+---------------+
    | t_name | count(t_name) |
    +--------+---------------+
    | 罗老师 |             1 |
    +--------+---------------+
    
    • 查询课程成绩小于70分的同学的学号, 姓名
    mysql> select s_id, s_name, scr_num from score left join student on student_id=student.s_id where scr_num < 70;
    +------+--------+---------+
    | s_id | s_name | scr_num |
    +------+--------+---------+
    |    1 | 雷军   |      60 |
    +------+--------+---------+
    1 row in set (0.00 sec)
    
    • 查询各科成绩最高和最低的分:以如下形式显示:课程名,最高分,最低分;
    mysql> select c_name, max(scr_num), min(scr_num) from score left join course on course_id=course.c_id group by c_name;
    +--------+--------------+--------------+
    | c_name | max(scr_num) | min(scr_num) |
    +--------+--------------+--------------+
    | C      |           60 |           60 |
    | Go     |           80 |           80 |
    | Python |           70 |           70 |
    +--------+--------------+--------------+
    3 rows in set (0.00 sec)
    
    
    • 查询姓“雷”的学生名单
    mysql> select * from student where s_name like '雷%';
    +------+--------+--------+----------+
    | s_id | s_name | gender | class_id |
    +------+--------+--------+----------+
    |    1 | 雷军   | 男     |        1 |
    +------+--------+--------+----------+
    1 row in set (0.00 sec)
    
    • 查询每门课程的平均成绩,结果按平均成绩升序排列,平均成绩相同时,按课程号降序排列
    
    mysql> select c_name, avg(scr_num) from score left join course on course_id=course.c_id group by c_name order by avg(scr_num) asc, c_id desc;
    +--------+--------------+
    | c_name | avg(scr_num) |
    +--------+--------------+
    | C      |      60.0000 |
    | Python |      70.0000 |
    | Go     |      80.0000 |
    +--------+--------------+
    3 rows in set (0.00 sec)
    
    • 查询课程编号为3且课程成绩在70分以上的学生的学号和姓名
    mysql> select s_id, s_name from score left join student on student_id=student.s_id left join course on course_id=course.c_id where c_id=3 and scr_num > 70;
    +------+--------+
    | s_id | s_name |
    +------+--------+
    |    1 | 雷军   |
    +------+--------+
    1 row in set (0.00 sec)
    
    • 查询“2”课程分数小于70,按分数降序排列的同学学号
    mysql> select s_id from score left join student on student_id=student.s_id left join course on course_id=course.c_id where c_id=2 and scr_num < 70 order by scr_num desc;
    +------+
    | s_id |
    +------+
    |    1 |
    +------+
    1 row in set (0.00 sec)
    
    • 删除学号为“2”的同学的“1”课程的成绩
    
    mysql> delete from score where student_id=2 and course_id=1;
    Query OK, 1 row affected (0.00 sec)
    
    mysql> select * from score;
    +--------+------------+-----------+---------+
    | scr_id | student_id | course_id | scr_num |
    +--------+------------+-----------+---------+
    |      1 |          1 |         3 |      80 |
    |      3 |          1 |         2 |      60 |
    +--------+------------+-----------+---------+
    2 rows in set (0.00 sec)
    
    
    • 删除学习“乔帮主”课的SC表记录
  • 相关阅读:
    在你设计中可能用到的20个杂志 PSD 原型
    Gradify
    CamanJS – 提供各种图片处理的 JavaScript 库
    免费素材:包含 250+ 组件的 DO UI Kit
    24个很赞的 Node.js 免费教程和在线指南
    Dynamics.js
    Page Scroll Effects
    Slides
    15款加速 Web 开发的 JavaScript 框架
    Wee – 为现代 Web 开发打造的 CSS 脚手架
  • 原文地址:https://www.cnblogs.com/bigb/p/11769012.html
Copyright © 2020-2023  润新知