mysql之连接查询小作业
#数据准备
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之连接查询小作业的更多相关文章
- MySql的连接查询
类似于oracle的连接查询,mysql连接查询也有左外连接.右外连接.内连接查询.但是,不同的是没有直接 的全外连接查询. 这里介绍MySql的连接查询: 这里已两张表为例:STUDENT 表 和 ...
- MySQL查询优化:连接查询排序limit
MySQL查询优化:连接查询排序limit(join.order by.limit语句) 2013-02-27 个评论 收藏 我要投稿 MySQL查询优化:连接查询排序 ...
- 【杂记】mysql 左右连接查询中的NULL的数据筛选问题,查询NULL设置默认值,DATE_FORMAT函数
MySQL左右连接查询中的NULL的数据筛选问题 xpression 为 Null,则 IsNull 将返回 True:否则 IsNull 将返回 False. 如果 expression 由多个变量 ...
- MySQL常见连接查询
在实际应用中,由于不同的业务需求,一般的select查询语句无法满足要求.所以就需要了解一些MySQL的高级查询方式 内连接 inner join 典型的连接查询,有相等(=)连接和不等(<&g ...
- Mysql表连接查询
原文地址: https://www.cnblogs.com/qiuqiuqiu/p/6442791.html 1.内联接(典型的联接运算,使用像 = 或 <> 之类的比较运算符).包括相等 ...
- MySQL 表记录查询小练习
表记录查询小练习 查看岗位是teacher的员工姓名.年龄 查看岗位是teacher且年龄大于26岁的员工姓名.年龄 查看岗位是teacher且薪资在12000-16000范围内的员工姓名.年龄.薪资 ...
- mysql(连接查询和数据库设计)
--创建学生表 create table students ( id int unsigned not null auto_increment primary key, name varchar(20 ...
- 【mysql】连接查询
- MySQL之连接查询
主要是多表查询和连接查询
随机推荐
- java 5线程中 Semaphore信号灯,CyclicBarrier类,CountDownLatch计数器以及Exchanger类使用
先来讲解一下Semaphore信号灯的作用: 可以维护当前访问自身的线程个数,并提供了同步机制, 使用semaphore可以控制同时访问资源的线程个数 例如,实现一个文件允许的并发访问数. 请看下面 ...
- 使用jmeter+ant进行接口自动化测试(数据驱动)之二:利用apache-ant执行测试用例并生成HTML格式测试报告
在 使用jmeter+ant进行接口自动化测试(数据驱动)之一 介绍了如何使用csv文件来批量管理接口 本次接着介绍如何利用apache-ant执行测试用例并生成HTML格式测试报告 ①下载安装 ap ...
- Archlinux安装和使用技巧
一 准备工作 1 文件下载及启动盘制作 文件可以在https://mirrors.ustc.edu.cn/,这是个中科大的镜像网,选择如下: 下载完成后,就是制作一个启动盘,我使用的是Linux下强 ...
- router问题
var http = require("http"); var router = require("./router.js"); //创建服务器 var ser ...
- mongodb 复制(副本集)
复制(副本集) 什么是复制 复制提供了数据的冗余备份,并在多个服务器上存储数据副本,提高了数据的可用性,并可以保证数据的安全性 复制还允许从硬件故障和服务中断中恢复数据 为什么要复制 数据备份 数据灾 ...
- RxSwift(一)
文/iOS_Deve(简书作者) 原文链接:http://www.jianshu.com/p/429b5160611f 著作权归作者所有,转载请联系作者获得授权,并标注"简书作者" ...
- sql 多条记录插入
--多条记录插入,用逗号分开值. INSERT dbo.studentinfor ( id, name, class, age, hpsw ) ', -- id - nvarchar(50) N'te ...
- JAVA_SE基础——33.this关键字的练习
需求:使用java定义的一个人类,人具备 id ,name ,age 三个属性,还具备一个比较年龄的方法. 要求:必须要写上构造函数,构造函数也必须要使用上this关键字. class Person{ ...
- windows 10下通过python3.6成功搭建jupyter 服务器
最近通过python学习爬虫技术,发现一个工具jupyter notebook很不错,该工具明显优势通过浏览器可以输入多行python代码,支持在线运行以及运行结果保存功能,在线验证python小模块 ...
- 我自己总结的C#开发命名规范整理了一份
我自己总结的C#开发命名规范整理了一份 标签: 开发规范文档标准语言 2014-06-27 22:58 3165人阅读 评论(1) 收藏 举报 分类: C#(39) 版权声明:本文为博主原创文章, ...