MySQL(作业练习)
day59
参考:http://www.cnblogs.com/wupeiqi/p/5748496.html
现有数据库
/*
Navicat Premium Data Transfer Source Server : localhost
Source Server Type : MySQL
Source Server Version : 50624
Source Host : localhost
Source Database : sqlexam Target Server Type : MySQL
Target Server Version : 50624
File Encoding : utf-8 Date: 10/21/2016 06:46:46 AM
*/ SET NAMES utf8;
SET FOREIGN_KEY_CHECKS = 0; -- ----------------------------
-- Table structure for `class`
-- ----------------------------
DROP TABLE IF EXISTS `class`;
CREATE TABLE `class` (
`cid` int(11) NOT NULL AUTO_INCREMENT,
`caption` varchar(32) NOT NULL,
PRIMARY KEY (`cid`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8; -- ----------------------------
-- Records of `class`
-- ----------------------------
BEGIN;
INSERT INTO `class` VALUES ('', '三年二班'), ('', '三年三班'), ('', '一年二班'), ('', '二年九班');
COMMIT; -- ----------------------------
-- Table structure for `course`
-- ----------------------------
DROP TABLE IF EXISTS `course`;
CREATE TABLE `course` (
`cid` int(11) NOT NULL AUTO_INCREMENT,
`cname` varchar(32) NOT NULL,
`teacher_id` int(11) NOT NULL,
PRIMARY KEY (`cid`),
KEY `fk_course_teacher` (`teacher_id`),
CONSTRAINT `fk_course_teacher` FOREIGN KEY (`teacher_id`) REFERENCES `teacher` (`tid`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8; -- ----------------------------
-- Records of `course`
-- ----------------------------
BEGIN;
INSERT INTO `course` VALUES ('', '生物', ''), ('', '物理', ''), ('', '体育', ''), ('', '美术', '');
COMMIT; -- ----------------------------
-- Table structure for `score`
-- ----------------------------
DROP TABLE IF EXISTS `score`;
CREATE TABLE `score` (
`sid` int(11) NOT NULL AUTO_INCREMENT,
`student_id` int(11) NOT NULL,
`course_id` int(11) NOT NULL,
`num` int(11) NOT NULL,
PRIMARY KEY (`sid`),
KEY `fk_score_student` (`student_id`),
KEY `fk_score_course` (`course_id`),
CONSTRAINT `fk_score_course` FOREIGN KEY (`course_id`) REFERENCES `course` (`cid`),
CONSTRAINT `fk_score_student` FOREIGN KEY (`student_id`) REFERENCES `student` (`sid`)
) ENGINE=InnoDB AUTO_INCREMENT=53 DEFAULT CHARSET=utf8; -- ----------------------------
-- Records of `score`
-- ----------------------------
BEGIN;
INSERT INTO `score` VALUES ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', '');
COMMIT; -- ----------------------------
-- Table structure for `student`
-- ----------------------------
DROP TABLE IF EXISTS `student`;
CREATE TABLE `student` (
`sid` int(11) NOT NULL AUTO_INCREMENT,
`gender` char(1) NOT NULL,
`class_id` int(11) NOT NULL,
`sname` varchar(32) NOT NULL,
PRIMARY KEY (`sid`),
KEY `fk_class` (`class_id`),
CONSTRAINT `fk_class` FOREIGN KEY (`class_id`) REFERENCES `class` (`cid`)
) ENGINE=InnoDB AUTO_INCREMENT=17 DEFAULT CHARSET=utf8; -- ----------------------------
-- Records of `student`
-- ----------------------------
BEGIN;
INSERT INTO `student` VALUES ('', '男', '', '理解'), ('', '女', '', '钢蛋'), ('', '男', '', '张三'), ('', '男', '', '张一'), ('', '女', '', '张二'), ('', '男', '', '张四'), ('', '女', '', '铁锤'), ('', '男', '', '李三'), ('', '男', '', '李一'), ('', '女', '', '李二'), ('', '男', '', '李四'), ('', '女', '', '如花'), ('', '男', '', '刘三'), ('', '男', '', '刘一'), ('', '女', '', '刘二'), ('', '男', '', '刘四');
COMMIT; -- ----------------------------
-- Table structure for `teacher`
-- ----------------------------
DROP TABLE IF EXISTS `teacher`;
CREATE TABLE `teacher` (
`tid` int(11) NOT NULL AUTO_INCREMENT,
`tname` varchar(32) NOT NULL,
PRIMARY KEY (`tid`)
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8; -- ----------------------------
-- Records of `teacher`
-- ----------------------------
BEGIN;
INSERT INTO `teacher` VALUES ('', '张磊老师'), ('', '李平老师'), ('', '刘海燕老师'), ('', '朱云海老师'), ('', '李杰老师');
COMMIT; SET FOREIGN_KEY_CHECKS = 1; 表结构和数据
题目与答案:
第2题
#2、查询“生物”课程比“物理”课程成绩高的所有学生的学号; #先连表 课程和成绩
select * from score left join course on score.course_id = course.cid; #选出生物的
select * from score left join course on score.course_id = course.cid where course.cname = "生物"; #从中选出课程id,学生id,课程及分数
select score.sid, score.student_id, course.cname,score.num from score left join course on score.course_id = course.cid where course.cname = "生物"; select score.sid, score.student_id, course.cname,score.num from score left join course on score.course_id = course.cid where course.cname = "物理"; #只能左右比较,所以 根据学生id左右连接 ,将生物与物理连接
select * from
(select score.sid, score.student_id, course.cname,score.num from score left join course on score.course_id = course.cid where course.cname = "生物") as A
inner join #存在空不显示,
(select score.sid, score.student_id, course.cname,score.num from score left join course on score.course_id = course.cid where course.cname = "物理") as B
on A.student_id = B.student_id; #根据条件进行选择
select * from
(select score.sid, score.student_id, course.cname,score.num from score left join course on score.course_id = course.cid where course.cname = "生物") as A
inner join #存在空不显示,
(select score.sid, score.student_id, course.cname,score.num from score left join course on score.course_id = course.cid where course.cname = "物理") as B
on A.student_id = B.student_id
where A.num > B.num; #只要学号
select A.student_id from
(select score.sid, score.student_id, course.cname,score.num from score left join course on score.course_id = course.cid where course.cname = "生物") as A
inner join #存在空不显示,
(select score.sid, score.student_id, course.cname,score.num from score left join course on score.course_id = course.cid where course.cname = "物理") as B
on A.student_id = B.student_id
where A.num > B.num; -- #选出有生物和物理的信息
-- select * from score left join course on score.course_id = course.cid where course.cname = "生物" or course.cname = "物理";
--
-- #从中选出课程id,学生id,课程及分数
-- select score.sid, score.student_id, course.cname,score.num from score left join course on score.course_id = course.cid where course.cname = "生物" or course.cname = "物理";
--
第5题
#查询姓李的老师的个数
select count(tid) from teacher where tname like '李%'
第6题
#6、查询没学过“李平”老师课的同学的学号、姓名; #先连接teacher和course
select * from course left join teacher on course.teacher_id = teacher.tid; #挑出李平老师的课
select * from course left join teacher on course.teacher_id = teacher.tid where teacher.tname = '李平老师'; #选出其中的李平课程的id 2,4
select course.cid from course left join teacher on course.teacher_id = teacher.tid where teacher.tname = '李平老师'; #选择选过李平课的学生
select * from score where course_id in
(select course.cid from course left join teacher on course.teacher_id = teacher.tid where teacher.tname = '李平老师'); #选过李平课的学生ID
select student_id from score where course_id in
(select course.cid from course left join teacher on course.teacher_id = teacher.tid where teacher.tname = '李平老师')
group by student_id; #到学生表中排除出以上ID
select * from student where sid not in
(
select student_id from score where course_id in
(select course.cid from course left join teacher on course.teacher_id = teacher.tid where teacher.tname = '李平老师')
group by student_id
); #最终选出学号和姓名
select student.sid, student.sname from student where sid not in
(
select student_id from score where course_id in
(select course.cid from course left join teacher on course.teacher_id = teacher.tid where teacher.tname = '李平老师')
group by student_id
);
第7题
#7、查询学过“001”课程并且也学过编号“002”课程的同学的学号、姓名; #选出学过 1和2 课程的学生信息
select * from score where course_id = 1 or course_id = 2 ; #根据 学号进行分组
select student_id from score where course_id = 1 or course_id = 2 GROUP BY student_id having count(course_id)>1;#选了1或者2的课同学,大于2门课的说明是1和2都选了 #学生id已知, 匹配student名字
select student.sid,student.sname from
(select student_id from score where course_id = 1 or course_id = 2 GROUP BY student_id having count(course_id)>1) as A
left join student on A.student_id = student.sid;
第8题
#8、查询学过“李平”老师所教的所有课的同学的学号、姓名; #先拿“李平"课程id 老师和课程先连表
select * from course left join teacher on teacher.tid = course.teacher_id; #拿出课程id
select course.cid from course left join teacher on teacher.tid = course.teacher_id where teacher.tname = "李平老师";
#2,4 #根据2,4从score中选出学生信息
select * from score where course_id in
(select course.cid from course left join teacher on teacher.tid = course.teacher_id where teacher.tname = "李平老师");
#其中这些学生都选了李平的课 #选出李平的课都上的学生
select student_id from score where course_id in
(select course.cid from course left join teacher on teacher.tid = course.teacher_id where teacher.tname = "李平老师")
group by student_id having count(course_id) = (select count(course.cid) from course left join teacher on teacher.tid = course.teacher_id where teacher.tname = "李平老师");
#等于李老师的课程数量即为上了老师的所有课
第10题
#10、查询有课程成绩小于60分的同学的学号、姓名; #找到有成绩小于60的学生
select * from score where num < 60; #找到有成绩小于60的学生的id
select student_id from score where num < 60; #并根据学号分组,因为有重复id出现,挂了多科的
select student_id from score where num < 60 GROUP BY student_id;
select distinct student_id from score where num < 60;#去重第二个方法 #和student进行连表
select student.sid, student.sname from (select student_id from score where num < 60 GROUP BY student_id) as A left join student on A.student_id = student.sid; #第二个方法
select sid,sname from student where sid in (
select distinct student_id from score where num < 60
)
第11题
#11、查询没有学全所有课的同学的学号、姓名; #获得课程总数
select count(cname) from course;
#select count(cid) from course;#主键速度更快 #每个学生id的上的课数, 如果不写 group by student_id会报错因为是根据学生id分组的
select student_id, count(course_id) as course_num from score group by student_id; #找出未选满的学生id
select student_id from
(select student_id, count(course_id) as course_num from score group by student_id) as A
where A.course_num not in (select count(cname) from course); #对应名字
select sid,sname from student where sid in
(
select student_id from
(select student_id, count(course_id) as course_num from score group by student_id) as A
where A.course_num not in (select count(cname) from course)
);
第12题
#12、查询 至少有一门课与学号为“001”的同学所学相同的同学的学号和姓名;
#即和001一起上过课的 #选出了课程001号同学的课程id
select course_id from score where student_id = 1; #选出和001号一起学习的ID
select DISTINCT student_id from score where course_id in (select course_id from score where student_id = 1); select sid, sname from (select DISTINCT student_id from score where student_id !=1 and course_id in (select course_id from score where student_id = 1)) as A left join student on student.sid = A.student_id;
#还需排除自身
MySQL(作业练习)的更多相关文章
- Mysql 作业(Scheduler)
200 ? "200px" : this.width)!important;} --> 介绍 作业也叫做事件调度,其实它也就是一个时间触发器:它可以定义某个时间点执行指定的数 ...
- mysql定时任务/mysql作业
转自:https://www.jb51.net/article/138569.htm 详细参考:https://www.cnblogs.com/qlqwjy/p/7954175.html(事件& ...
- MySQL作业
创建作业事件 MONTH STARTS '2015-01-01 05:30:01' ON COMPLETION NOT PRESERVE ENABLE DO CALL sp_moveLoginReco ...
- mysql安装及基本操作(mysql作业)
1 官网下载,链接 https://www.mysql.com/downloads/ Download MySQL Community Server 默认为你选好了Mac OS X 平台 选择的是. ...
- 【MySQL作业】MySQL函数——美和易思系统信息函数和加密函数应用习题
点击打开所使用到的数据库>>> 1.显示当前 MySQL 服务器的版本信息和登录信息. MySQL 系统信息函数 version() 用于返回当前 MySQL 的版本号," ...
- 【MySQL作业】MySQL函数——美和易思日期和时间函数应用习题
点击打开所使用到的数据库>>> 1.采用尽可能多的方式显示当前系统日期和时间. 下列 SQL 语句可以显示当前系统的日期和时间: curdate() 和 current_date() ...
- 【MySQL作业】MySQL函数——美和易思字符串函数应用习题
点击打开所使用到的数据库>>> 1.将所有客户的姓名与电话以"-"作为分隔符进行连接显示. 使用 concat(s1,s2,-) 函数将所有客户的姓名与电话以&q ...
- 【MySQL作业】MySQL函数——美和易思数学函数和控制流函数应用习题
点击打开所使用到的数据库>>> 1.添加一条商品记录. 商品编码 goodsCode 商品名 goodsName 种类 category 单价 unitPrice 02005 夏 ...
- 【MySQL作业】DDL 和 DML——美和易思使用 DML 删除表数据应用习题
点击打开所使用到的数据库>>> 删除客户"刘一鸣". 执行 SQL 代码"delete from customer where cName=' 刘一鸣 ...
随机推荐
- LUOGU P4408 [NOI2003]逃学的小孩(树的直径)
题目描述 Chris家的电话铃响起了,里面传出了Chris的老师焦急的声音:“喂,是Chris的家长吗?你们的孩子又没来上课,不想参加考试了吗?”一听说要考试,Chris的父母就心急如焚,他们决定在尽 ...
- Jmeter运行过程中如何让Fiddler同时可以抓获到服务器的应答报文
在默认情况下,Jmeter运行过程中,Fiddler是抓不到对应的应答报文的. 但是,在某些时候,我们希望分析Jmeter执行失败的原因,想了解Jmeter获取到的应答报文是否有问题,就需要同服务器返 ...
- 11 Maven 灵活的构建
Maven 灵活的构建 一个优秀的构建系统必须足够灵活,它应该能够让项目在不同的环境下都能成功地构建.例如,典型的项目都会有开发环境.测试环境和产品环境,这些环境的数据库配置不尽相同,那么项目构建的时 ...
- HTML5 history详解
最近研究vue-router单页转跳而不向服务器请求的原理, 主要是HTML5 history以及hash的应用,支持history时使用history模式 下面详细学习了一下常用的history相关 ...
- ubuntu系统下安装pyspider:使用supervisord启动并管理pyspider进程配置及说明
首先感谢segmentfault.com的“imperat0r_”用户的文章和新浪的“小菜一碟”用户的文章.这是他们的配置文件.我参考也写了一个,在最后呢. 重点说明写在前面.本人用superviso ...
- UUID含义及ubuntu配置系统默认JDK
UUID含义是通用唯一识别码(Universally Unique Identifier) GUID是一个128位长的数字,一般用16进制表示.算法的核心思想是结合机器的网卡.当地时间.一个随即数来生 ...
- Java知识系列 -- 反射
原理 要想理解 Java 反射,首先要弄清类的加载过程. 比如这行代码 Person p = new Person();. 我们想要创建一个 Person 对象,并用 p 作为对象的引用. 在 Jav ...
- Devexpress VCL Build v2013 vol 13.2.5 发布
支持xe6 了,但是承诺的功能在哪里? What's New in 13.2.5 (VCL Product Line) New Major Features in 13.2 What's New ...
- 7.计算N元等式[穷举]
穷举的一种应用,计算x+2y+3z=50的非负整数解.先约束每个变量的最大值,x=50,y=25,z=50/3. #include <iostream> using namespace s ...
- Bezier曲线
1. 学习网址 http://give.zju.edu.cn/cgcourse/new/book/8.2.htm