#数据准备
drop table if exists class;
create table class(
    class_no int(2) unsigned zerofill primary key auto_increment comment '班级编号',
    class_name varchar(30) not null comment '班级名称'
);
insert into class values(1, '培优班');
insert into class values(2, '普通班');
insert into class values(3, '进阶班');

drop table if exists student;
create table student(
    stu_no int(2) unsigned zerofill primary key auto_increment comment '学员编号',
    stu_name varchar(30) not null comment '学员姓名',
    stu_sex varchar(3) not null comment '学员性别',
    stu_age tinyint(2) unsigned zerofill comment '学员年代',
    grade double(5,2) zerofill comment '成绩',
    class_no int(2) unsigned zerofill comment '所在班级编号',
    foreign key(class_no) references class(class_no)
);
insert into student values(01, '李白', '男', 18, 60, 01);
insert into student values(02, '杜甫', '男', 20, 76, 01);
insert into student values(03, '张飞', '男', 32, 80, 02);
insert into student values(04, '韩信', '男', 26, 98, 02);
insert into student values(05, '了龙', '男', 27, 56, 02);
insert into student values(06, '大乔', '女', 17, 88, 01);
insert into student values(07, '小乔', '女', 16, 96, 01);
insert into student values(08, '小乔', '女', 16, 90, 01);
insert into student values(09, '关哥', '男', 32, 80, 02);
insert into student values(10, '刘备', '男', 36, 98, null);
alter table student drop foreign key `student_ibfk_1`;
*********************************************************************************************************************************************

1: 查询出‘培优班’的学员
  // 子查询
  select * from student where class_no = (select class_no from class where class_name = "培优班");
  // 内连接
  select * from student inner join class on student.class_no = class.class_no and class_name = "培优班";
  // 自然连接
  select * from student natural join class where class_name = "培优班";

2: 查询出‘普通班’成绩高于85分学员
  select * from student where class_no = (select class_no from class where class_name = "普通班") and grade > 85;
  select * from student inner join class on student.class_no = class.class_no and class_name = "普通班" and grade > 85;
  select * from student natural join class where class_name = "普通班" and grade > 85;

3: 写出一个迪卡尔集的查询结果
  select * from student cross join class;
  select * from student inner join class;

4: 查询出每一个班级的平均分
  // 包含班级号为null的结果
  select class_no,avg(grade) from student group by class_no;
  // 不包含班级号为null的结果
  select class_no,avg(grade) from student inner join class using(class_no) group by class_no; // 不包括class_no为null的结果

5: 查询出每一个学员的姓名和所在的班级名称
  select stu_name,class_name from student inner join class using(class_no);
  select stu_name,class_name from student inner join class on student.class_no=class.class_no;
  select class_name,stu_name from student natural join class;

6: 查询出培优班的最低分是多少
  select min(grade) from student where class_no = (select class_no from class where class_name = "培优班");
  select min(grade) from student inner join class on class.class_no = student.class_no and class_name = "培优班";
  select min(grade) from student natural join class where class_name = "培优班";

7: 查询出培优班成绩最差的学员信息(成绩最差的不一定是一个人)
  select * from student where class_no = (select class_no from class where class_name = "培优班") and grade = (select min(grade) from student where class_no = (select class_no from class where class_name = "培优班"));
  select * from student where (class_no,grade) = (select class_no,min(grade) from student natural join class where class_name = "培优班");

8: 查询出普通班成绩最好的学员信息
  select * from student natural join class where class_name = "普通班" order by grade desc limit 1;
  (改下第七题的条件就好)

9: 查询出成绩最好的学员的姓名 以及 他们的班级名称
  // 结果为多条记录的查询
  select stu_name,class_name from student natural left join class where grade = (select max(grade) from student);

10: 查询出男女学员人数的差值
  select (select count(*) from student where stu_sex = "男") - (select count(*) from student where stu_sex = "女") as "男女人数的差值";

mysql之连接查询小作业的更多相关文章

  1. MySql的连接查询

    类似于oracle的连接查询,mysql连接查询也有左外连接.右外连接.内连接查询.但是,不同的是没有直接 的全外连接查询. 这里介绍MySql的连接查询: 这里已两张表为例:STUDENT 表 和 ...

  2. MySQL查询优化:连接查询排序limit

    MySQL查询优化:连接查询排序limit(join.order by.limit语句) 2013-02-27      个评论       收藏    我要投稿   MySQL查询优化:连接查询排序 ...

  3. 【杂记】mysql 左右连接查询中的NULL的数据筛选问题,查询NULL设置默认值,DATE_FORMAT函数

    MySQL左右连接查询中的NULL的数据筛选问题 xpression 为 Null,则 IsNull 将返回 True:否则 IsNull 将返回 False. 如果 expression 由多个变量 ...

  4. MySQL常见连接查询

    在实际应用中,由于不同的业务需求,一般的select查询语句无法满足要求.所以就需要了解一些MySQL的高级查询方式 内连接 inner join 典型的连接查询,有相等(=)连接和不等(<&g ...

  5. Mysql表连接查询

    原文地址: https://www.cnblogs.com/qiuqiuqiu/p/6442791.html 1.内联接(典型的联接运算,使用像 = 或 <> 之类的比较运算符).包括相等 ...

  6. MySQL 表记录查询小练习

    表记录查询小练习 查看岗位是teacher的员工姓名.年龄 查看岗位是teacher且年龄大于26岁的员工姓名.年龄 查看岗位是teacher且薪资在12000-16000范围内的员工姓名.年龄.薪资 ...

  7. mysql(连接查询和数据库设计)

    --创建学生表 create table students ( id int unsigned not null auto_increment primary key, name varchar(20 ...

  8. 【mysql】连接查询

  9. MySQL之连接查询

    主要是多表查询和连接查询

随机推荐

  1. Git详细教程(2)---多人协作开发

    Git可以完成两件事情: 1. 版本控制 2.多人协作开发 如今的项目,规模越来越大,功能越来越多,需要有一个团队进行开发. 如果有多个开发人员共同开发一个项目,如何进行协作的呢. Git提供了一个非 ...

  2. Hibernate学习笔记二

    Hibernate持久化类的编写规则 Hibernate是持久层的ORM映射框架,专注于数据的持久化工作.所谓持久化,就是将内存中的数据永久存储到关系型数据库中. 持久化类 一个java类与数据库表建 ...

  3. hackme.inndy.tw - pyyy - Writeup

    hackme.inndy.tw - pyyy - Writeup 0x01 反编译 1.第一次尝试的时候我直接在线反编译,部分结果如下. for (i, f) in enumerate(F): n = ...

  4. MySQL之数据库和表的基本操作(建立表、删除表、向表中添加字段)

    介绍关于数据库和表的一些基本操作 添加字段.给字段添加注释 ); ) COMMENT '统一社会信用代码录入单位'; ,) 更改字段类型 ,) COMMENT '一头签收,@0或空不用,1必须'; 有 ...

  5. 《构建之法》教学笔记——Python中的效能分析与几个问题

    <构建之法:现代软件工程>中第2章对效能分析进行了介绍,基于的工具是VSTS.由于我教授的学生中只有部分同学选修了C#,若采用书中例子讲解,学生可能理解起来比较困难.不过所有这些学生都学习 ...

  6. Beta冲刺NO.1

    Beta冲刺 第一天 1. 昨天的困难 由于今天还是第一天,所以暂时没有昨天的困难. 2. 今天解决的进度 潘伟靖: 对代码进行了review 1.将某些硬编码改为软编码 2.合并了一些方法,简化代码 ...

  7. python的迭代器、生成器、装饰器

    迭代器.生成器.装饰器 在这个实验里我们学习迭代器.生成器.装饰器有关知识. 知识点 迭代器 生成器 生成器表达式 闭包 装饰器 实验步骤 1. 迭代器 Python 迭代器(Iterators)对象 ...

  8. linux cenots7安装mysql

        1.下载mysql 下载的话先确认好版本. system:centos7 mysql:5.7 下面的版本自己选择,一般是86位的. 下载好的文件 2.上传到服务器 soft文件夹,终端也进入了 ...

  9. JAVA_SE基础——17.方法的重载

    方法重载: 方法重载就是方法名称重复,加载参数不同. 具体规范: 一.方法名一定要相同. 二.方法的参数表必须不同,包括参数的类型或个数,以此区分不同的方法体. 1.如果参数个数不同,就不管它的参数类 ...

  10. Java Jar包压缩、解压使用指南

    什么是jar包 JAR(Java Archive)是Java的归档文件,它是一种与平台无关的文件格式,它允许将许多文件组合成一个压缩文件. 如何打/解包 使用jdk/bin/jar.exe工具,配置完 ...