#数据准备
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. freemarker 类型转换

    操作字符串函数  1. substring(start,end)从一个字符串中截取子串  start:截取子串开始的索引,start必须大于等于0,小于等于endend: 截取子串的长度,end必须大 ...

  2. 让Myeclipse自动生成的get set方法 自动加上文本注释,并且注释内容包含字段中我们加的文档注释

    在进行编码写实体类的时候发现,一个实体类有好多的字段要进行注释,他们都是私有的不能直接访问,我们在写的时候加入的文档注释也起不到效果,但是自动生成的get,set方法的文档注释有不符合我们要求(没有包 ...

  3. MySQL_执行计划详细说明

          1 简要说明 id 表格查询的顺序编号. 降序查看,id相同的从上到下查查看. id可以为null ,当table为( union ,m,n )类型的时候,id为null,这个时候,id的 ...

  4. C语言程序设计(基础)- 第7周作业(新)

    要求一(25经验值) 完成PTA中题目集名为<usth-C语言基础-第七周作业>和<usth-C语言基础-12周PTA作业>中的所有题目. 注意1:<usth-C语言基础 ...

  5. 庖丁解牛Linux内核学习笔记(1)--计算机是如何工作的

    存储程序计算机模型 冯诺依曼体系结构 冯诺依曼体系结构是存储程序计算机,什么叫存储程序计算机?从硬件角度说,假设有cpu和内存,两者通过总线连接,在cpu内部有一个寄存器叫ip(instruction ...

  6. 20162302 实验一《Java开发环境的熟悉》实验报告

    实 验 报 告 课程:程序设计与数据结构 姓名:杨京典 班级:1623 学号:20162302 实验名称:Java开发环境的熟悉 实验器材:装有Ubuntu的联想拯救者80RQ 实验目的与要求:1.使 ...

  7. 结对编程作业——四则运算GUI程序

    毛忠庆 201421122088 赵嘉楠 201421122065 源代码存放位置:https://gitee.com/ouwen0819/SiZeYunSuan.git 题目描述 使用 -n 参数控 ...

  8. 团队作业4——第一次项目冲刺(Alpha版本)2017.11.14

    第一次会议:2017-11-14 额--这几天比较忙,忘记上传了,今天补上 先上个图,O(∩_∩)O哈哈: 会议主要内容: 1. 讨论整体框架 2. 个人具体分工 3. 代码统一 具体分工: 成员 计 ...

  9. bzoj千题计划220:bzoj3938: Robot

    http://www.lydsy.com/JudgeOnline/problem.php?id=3938 以时间为x轴,以距离为y轴,那么每个机器人的行走路径就是一条折线 把折线分段加入线段树里,然后 ...

  10. JAVA_SE基础——20.数组的常见操作

    1.遍历数组 使用for循环来遍历数组 代码如下: public class Ergodic { public static void main(String[] args) { int[] arr ...