python学习之老男孩python全栈第九期_数据库day003 -- 作业
数据库:
class:
student:
teacher:
score:
/*
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;
表结构和数据
1. 成绩表中所有大于60分的数据
SELECT * from score where num>60;
2. 查询每个老师教多少门课
SELECT teacher_id,COUNT(cid) FROM course GROUP BY teacher_id;
3. 列出课程与对应老师
SELECT * FROM course LEFT JOIN teacher on course.teacher_id=teacher.tid;
4. 列出学生与对应班级
SELECT * FROM student LEFT JOIN class on student.class_id=class.cid;
5. 列出男生的个数和女生的个数
SELECT gender,COUNT(gender) from student GROUP BY gender;
6. 查询“生物”课程比“物理”课程成绩高的所有学生的学号
先拿到所有的生物成绩
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='物理';
接着输出生物成绩大于物理成绩的数据
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;
最后,把* 改为A.student_id 即可
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;
7. 查询平均成绩大于60分的同学的学号、姓名和平均成绩
select student_id,student.sname,avg(num) from score LEFT JOIN student ON score.student_id=student.sid GROUP BY student_id HAVING AVG(num)>60;
用临时表做:
SELECT B.student_id,student.sname,B.avg_num from (select student_id,avg(num) as avg_num from score GROUP BY student_id HAVING AVG(num)>60) as B LEFT join student on B.student_id=student.sid;
8. 查询所有同学的学号、姓名、选课数、总成绩
select student.sid,student.sname,COUNT(score.course_id),sum(score.num) from student LEFT JOIN score ON student.sid=score.student_id GROUP BY student.sid;
9. 查询姓“李”的老师的个数
select count(tid) from teacher where tname like '李%';
10. 查询没学过“李平”老师课的同学的学号、姓名
先查询选过李平老师课的学生ID
SELECT
student_id
FROM
score
WHERE
course_id IN ( SELECT course.cid FROM teacher LEFT JOIN course ON teacher.tid = course.teacher_id 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 teacher LEFT JOIN course ON teacher.tid = course.teacher_id WHERE teacher.tname = '李平老师' )
GROUP BY
student_id
)
python学习之老男孩python全栈第九期_数据库day003 -- 作业的更多相关文章
- python学习之老男孩python全栈第九期_数据库day002 -- 作业 (数据库为day001创建的数据库)
1.自行创建测试数据 对score表进行优化: 添加数据: 2.查询“生物”课程比“物理”课程成绩高的所有学生的学号: 为了方便做题,额外增加几条数据 查询: 3.查询平均成绩大于60分的同学的学号和 ...
- python学习之老男孩python全栈第九期_数据库day004 -- 作业
https://www.cnblogs.com/YD2018/p/9451809.html 11. 查询学过“001”并且也学过编号“002”课程的同学的学号.姓名 select student.si ...
- python学习之老男孩python全栈第九期_数据库day003知识点总结 —— MySQL数据库day3
复习: 1. 增 insert into xx(name) values('root'),('xxx'); insert into xx(name) select id from tb1; 2. 自增 ...
- python学习之老男孩python全栈第九期_数据库day001 -- 作业
创建如图所示数据库: 创建过程: 查看数据库,创建数据库 db1,再查看一下数据库 进入数据库,查看一下表 接着再创建一个class表 发现增加了重复数据,因此要把第二个修改一下 修改完数据之 ...
- python学习之老男孩python全栈第九期_数据库day005知识点总结 —— MySQL数据库day5
三. MySQL视图(不常用) 给某个查询语句设置个别名(视图名),日后方便使用 - 创建: create view 视图名 as SQL; PS:视图是虚拟的 - 修改: alter view 视图 ...
- python学习之老男孩python全栈第九期_数据库day004知识点总结 —— MySQL数据库day4
复习: 1. MySQL:文件管理的软件 2. 三部分: - 服务端 - SQL语句 - 客户端 3. 客户端: - MySQL - navicat 4. 授权操作: - 用户操作 - 授权操作 5. ...
- python学习之老男孩python全栈第九期_数据库day002知识点总结 —— MySQL数据库day2(全部)
一. 复习1. MySQL: - 服务端 - 客户端2. 通信交流 - 授权 - SQL语句 - 数据库 创建数据库: create database db1 default charset utf8 ...
- python学习之老男孩python全栈第九期_数据库day001知识点总结 —— MySQL操作数据库以及数据表、基本数据类型、基本增删改查、外键定义以及创建
一. 学习SQL语句规则以及外键 1. 操作文件夹 create database db2; 创建文件夹 create database db2 default charset utf8; 创建文件夹 ...
- python学习之老男孩python全栈第九期_第一次周末考试题(over)第三次添加完毕
day 6python基础数据类型考试题 考试时间:两个半小时 满分100分(80分以上包含80分及格) 一,基础题. 1. 简述变量命名规范(3分) 答:(1) 变量为数字,字母以及下划线的任意组合 ...
随机推荐
- 前端入门html(常用标签及标签分类)
day47 参考:https://www.cnblogs.com/liwenzhou/p/7988087.html 任何标签都有有三个属性:ID,class.style <!DOCTYPE ht ...
- Python(socketserver并发聊天)
day27 一个server与多个client聊天. server.py import socketserver class MyServer(socketserver.BaseRequestHand ...
- Good Bye 2017(送命场)
9815人数场,9500+围观神仙打架...断断续续打Codeforces也快有一年啦,第一次打Good Bye场,满怀前排膜tourist的心愿参加了这场送命场,虽然没看到tourist.不过还是得 ...
- position:absolute元素 怎样居中
<div style = 'height:20px;position:absolute;z-index:9999;top:0;left:0;right:0;margin:auto;'> & ...
- Centos之文件搜索命令find
find [搜索范围] [搜索条件] #搜索文件 find / -name install.log #避免大范围搜索,会非常耗费系统资源 #find是在系统当中搜索符合条件的文件名.如果需要匹配, 使 ...
- DIV居中的几种方法
1. body{ text-align:center; } 缺点:body内所有内容一并居中 2. .center{ position: fixed; left: 50%; } 缺点:需要设置posi ...
- Window History对象
History 对象属性 length 返回浏览器历史列表中的 URL 数量. History 对象方法 back() 加载 history 列表中的前一个 URL. window.history.b ...
- easyui 中iframe嵌套页面,大弹窗自适应居中的解决方法。$('#win').window()
easyui 中iframe嵌套页面,大弹窗自适应居中的解决方法.$('#win').window() 以下是左边栏和头部外层遮罩显示和隐藏方法 /*外层 遮罩显示*/ function wrapMa ...
- RDLC_部署到不同的浏览器
首先我用的是vs2015 的reportview插件 在数据库中应该配置报表的服务器地址,在项目中添加ReportViewer 插件,单独用一个页面显示接收报表 <form id="f ...
- Netty核心概念(9)之Future
1.前言 第7节讲解JAVA的线程模型中就说到了Future,并解释了为什么可以主线程可以获得线程池任务的执行后结果,变成一种同步状态.秘密就在于Java将所有的runnable和callable任务 ...