创建表结果和数据准备[直接执行即可]

 /*
Navicat MySQL Data Transfer Source Server : ftl1012
Source Server Version : 50617
Source Host : localhost:3306
Source Database : test_python Target Server Type : MYSQL
Target Server Version : 50617
File Encoding : 65001 Date: 2017-12-30 13:12:57
*/ 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
-- ----------------------------
INSERT INTO `class` VALUES ('', '三年二班');
INSERT INTO `class` VALUES ('', '三年三班');
INSERT INTO `class` VALUES ('', '一年二班');
INSERT INTO `class` VALUES ('', '二年九班'); -- ----------------------------
-- 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
-- ----------------------------
INSERT INTO `course` VALUES ('', '生物', '');
INSERT INTO `course` VALUES ('', '物理', '');
INSERT INTO `course` VALUES ('', '体育', '');
INSERT INTO `course` VALUES ('', '美术', ''); -- ----------------------------
-- 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
-- ----------------------------
INSERT INTO `score` VALUES ('', '', '', '');
INSERT INTO `score` VALUES ('', '', '', '');
INSERT INTO `score` VALUES ('', '', '', '');
INSERT INTO `score` VALUES ('', '', '', '');
INSERT INTO `score` VALUES ('', '', '', '');
INSERT INTO `score` VALUES ('', '', '', '');
INSERT INTO `score` VALUES ('', '', '', '');
INSERT INTO `score` VALUES ('', '', '', '');
INSERT INTO `score` VALUES ('', '', '', '');
INSERT INTO `score` VALUES ('', '', '', '');
INSERT INTO `score` VALUES ('', '', '', '');
INSERT INTO `score` VALUES ('', '', '', '');
INSERT INTO `score` VALUES ('', '', '', '');
INSERT INTO `score` VALUES ('', '', '', '');
INSERT INTO `score` VALUES ('', '', '', '');
INSERT INTO `score` VALUES ('', '', '', '');
INSERT INTO `score` VALUES ('', '', '', '');
INSERT INTO `score` VALUES ('', '', '', '');
INSERT INTO `score` VALUES ('', '', '', '');
INSERT INTO `score` VALUES ('', '', '', '');
INSERT INTO `score` VALUES ('', '', '', '');
INSERT INTO `score` VALUES ('', '', '', '');
INSERT INTO `score` VALUES ('', '', '', '');
INSERT INTO `score` VALUES ('', '', '', '');
INSERT INTO `score` VALUES ('', '', '', '');
INSERT INTO `score` VALUES ('', '', '', '');
INSERT INTO `score` VALUES ('', '', '', '');
INSERT INTO `score` VALUES ('', '', '', '');
INSERT INTO `score` VALUES ('', '', '', '');
INSERT INTO `score` VALUES ('', '', '', '');
INSERT INTO `score` VALUES ('', '', '', '');
INSERT INTO `score` VALUES ('', '', '', '');
INSERT INTO `score` VALUES ('', '', '', '');
INSERT INTO `score` VALUES ('', '', '', '');
INSERT INTO `score` VALUES ('', '', '', '');
INSERT INTO `score` VALUES ('', '', '', '');
INSERT INTO `score` VALUES ('', '', '', '');
INSERT INTO `score` VALUES ('', '', '', '');
INSERT INTO `score` VALUES ('', '', '', '');
INSERT INTO `score` VALUES ('', '', '', '');
INSERT INTO `score` VALUES ('', '', '', '');
INSERT INTO `score` VALUES ('', '', '', '');
INSERT INTO `score` VALUES ('', '', '', '');
INSERT INTO `score` VALUES ('', '', '', '');
INSERT INTO `score` VALUES ('', '', '', '');
INSERT INTO `score` VALUES ('', '', '', '');
INSERT INTO `score` VALUES ('', '', '', ''); -- ----------------------------
-- 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
-- ----------------------------
INSERT INTO `student` VALUES ('', '男', '', '理解');
INSERT INTO `student` VALUES ('', '女', '', '钢蛋');
INSERT INTO `student` VALUES ('', '男', '', '张三');
INSERT INTO `student` VALUES ('', '男', '', '张一');
INSERT INTO `student` VALUES ('', '女', '', '张二');
INSERT INTO `student` VALUES ('', '男', '', '张四');
INSERT INTO `student` VALUES ('', '女', '', '铁锤');
INSERT INTO `student` VALUES ('', '男', '', '李三');
INSERT INTO `student` VALUES ('', '男', '', '李一');
INSERT INTO `student` VALUES ('', '女', '', '李二');
INSERT INTO `student` VALUES ('', '男', '', '李四');
INSERT INTO `student` VALUES ('', '女', '', '如花');
INSERT INTO `student` VALUES ('', '男', '', '刘三');
INSERT INTO `student` VALUES ('', '男', '', '刘一');
INSERT INTO `student` VALUES ('', '女', '', '刘二');
INSERT INTO `student` VALUES ('', '男', '', '刘四'); -- ----------------------------
-- 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
-- ----------------------------
INSERT INTO `teacher` VALUES ('', '张磊老师');
INSERT INTO `teacher` VALUES ('', '李平老师');
INSERT INTO `teacher` VALUES ('', '刘海燕老师');
INSERT INTO `teacher` VALUES ('', '朱云海老师');
INSERT INTO `teacher` VALUES ('', '李杰老师');

经验: join, 临时表的学习[in 的使用,右边只能输出一个]

-- 查询平均成绩大于60分的同学的学号和平均成绩,按照平均成绩降序排序 操作score张表关联student查姓名

方案一[FTL]:
select * from (select student_id, avg(num) from score group by student_id having avg(num) > 60) as T left join student on student.sid = T.student_id;
方案二:
select score.student_id, student.sname, avg(num),max(num),min(num) from score LEFT JOIN student on student.sid = score.student_id
GROUP BY score.student_id having avg(num) > 60 order by avg(num) desc;

-- 查询所有同学的学号、姓名、选课数、总成绩;

SELECT  a.sid, a.sname,
SUM(s.num) as zongchengji, count(s.course_id) as xuankeshu from score s
LEFT JOIN student a on a.sid = s.student_id GROUP BY s.student_id

-- 查询姓“李”的老师的个数;

方案一[FTL]:select count(tname) from teacher where tname like '李%';
方案二:select count(1) from teacher where tname like '李%';

-- 查询没学过“李平”老师课的同学的学号、姓名;

方案一[FTL]:局限性:不能查其他表中的内容
-- 没学过 ==> not int ==> sid not in 成绩表
-- 利用成绩表查找课程信息 ==>关联老师的信息
-- 李平老师 ==> where tname like '李平'
select student.sid, student.sname from student where sid not in
(
select student_id from score LEFT JOIN
course on score.course_id = course.cid
LEFT JOIN teacher on teacher.tid = course.teacher_id
where tname like '李平%'
); 方案二:
思路:
先查到“李平老师”老师教的所有课ID
获取选过课的所有学生ID
学生表中筛选
select * from student where sid not in (
select DISTINCT student_id from score where score.course_id in (
select cid from course left join teacher on course.teacher_id = teacher.tid where tname = '李平老师'
)
)

-- 查询学过“001”并且也学过编号“002”课程的同学的学号、姓名;

方案一[FTL]:
-- 选出课程为1或者2的课 ==> 关联student表查询
SELECT sid,sname from student where sid in
(select DISTINCT student_id from score where score.course_id in
(select cid from course where cid in (1,2))) 方案二:
SELECT sid, sname from
(SELECT student_id, count(student_id) FROM
(SELECT student_id, course_id FROM score WHERE course_id = 1 OR course_id = 2) AS B
GROUP BY student_id HAVING count(student_id) > 1
) as C LEFT JOIN student on student.sid = C.student_id; 方案三:
select student_id,sname from
(select student_id,course_id from score where course_id = 1 or course_id = 2) as B left join student on B.student_id = student.sid group by student_id HAVING count(student_id) > 1;

-- 查询没有学全所有课的同学的学号、姓名;

-- 查询没有学全所有课的同学的学号、姓名;
-- 所有课 ==> count(1) from course
-- 同学的学号、姓名 ==> sid, sname from student
-- 没有学全所有课 ==> count(course_id)
方案二:
SELECT student.sid, sname from student LEFT JOIN score on score.student_id = student.sid
group by student_id HAVING count(course_id) = (select count(1) from course)

-- 查询学过“李平老师“所教的所有课的同学的学号、姓名;

-- 查询学过“李平老师“所教的所有课的同学的学号、姓名;
-- 找到李平老师教过的课程ID
方案一[FTL]:
SELECT sid, sname FROM student where sid in (SELECT student_id FROM score WHERE course_id IN (SELECT cid FROM course LEFT JOIN teacher ON teacher_id = tid WHERE teacher.tname LIKE '%李平%' )) 方案二:
select sid, sname from (select student_id from score where course_id in (select cid from course left join teacher on course.teacher_id = teacher.tid where teacher.tname='李平老师') group by student_id)
as B left JOIN student on student.sid = B.student_id

-- 查询所有课程成绩小于60分的同学的学号、姓名;

-- 查询所有课程成绩小于60分的同学的学号、姓名;
方案一[FTL]:
select * from (
select student_id, num from score LEFT JOIN student on student.sid = score.student_id where num < 60 ORDER BY num asc ) as B LEFT JOIN student on B.student_id = student.sid

-- 查询至少有一门课与学号为“001”的同学所学相同的同学的学号和姓名

-- 查询至少有一门课与学号为“001”的同学所学相同的同学的学号和姓名
方案一[FTL_有错误,未解决]
select student.sid, sname from
(SELECT * from score where course_id in (
select course_id from score where student_id = 1
)) as B LEFT JOIN student on B.sid = student.sid
方案二:
思路:
获取 001 同学选择的所有课程
获取课程在其中的所有人以及所有课程
根据学生筛选,获取所有学生信息
再与学生表连接,获取姓名
select student_id, sname, count(course_id) from score
LEFT JOIN student on student.sid = score.student_id where student_id != 1 and course_id in (
select course_id from score where course_id = 1) GROUP BY student_id

-- 查询至少学过学号为“001”同学所有课数目相同的其他同学学号和姓名

-- 查询至少学过学号为“001”同学所有课数目相同的其他同学学号和姓名
-- 001 所学习的课程
-- 其他人的课 >= 001里面的课
方案一[FTL]:
select sid, sname from
(select student_id, course_id from score where course_id in
(select course_id from score where student_id = 1) group by student_id
HAVING count(1) = (select count(1) from score WHERE student_id = 1) ) as B
LEFT JOIN student on student.sid = B.student_id GROUP BY student_id

-- 查询和“002”号的同学学习的课程完全相同的其他同学学号和姓名;【高难度】

-- 查询和“002”号的同学学习的课程完全相同的其他同学学号和姓名;【高难度】
-- 002号学习的课程
-- count(1) == 002的课程count(1)
-- left JOIN student 拼接姓名和学号
-- 数量相同学生ID
select student_id, course_id, count(1) from score GROUP BY student_id having count(1) =
(select count(1) from score where student_id = 2 )
-- 课程相同
select student_id, course_id from score where course_id in
(select DISTINCT course_id from score where student_id = 2) GROUP BY student_id
-- 综合完成
select student_id,sname from score left join student on score.student_id = student.sid where student_id in (select student_id from score where student_id != 1 group by student_id HAVING count(course_id) = (select count(1) from score where student_id = 1)) and course_id in (select course_id from score where student_id = 1) group by student_id HAVING count(course_id) = (select count(1) from score where student_id = 1)

-- 删除学习“叶平”老师课的score表记录;

delete from score where course_id in (
select cid from course left join teacher on course.teacher_id = teacher.tid where teacher.name = '叶平') 

[更多参考] http://www.cnblogs.com/wupeiqi/articles/5729934.html  

[更多参考] http://www.cnblogs.com/wupeiqi/articles/5748496.html  

Mysql学习---SQL测试题之表结构的更多相关文章

  1. sql server 修改表结构

    文章来自http://blog.csdn.net/huwei2003/article/details/6076051 --修改数据库名称.表名称.字段名 --修改数据库名 sp_renamedb 'o ...

  2. MySQL优化四(优化表结构)

    body { font-family: Helvetica, arial, sans-serif; font-size: 14px; line-height: 1.6; padding-top: 10 ...

  3. Sql中获取表结构(字段名称,类型,长度,说明)

    Sql中获取表结构(字段名称,类型,长度,说明) SELECT TableName = OBJECT_NAME(c.object_id), ColumnsName = c.name, Descript ...

  4. SQL脚本修改表结构

    SQL脚本修改表结构 新建表:create table [表名]([自动编号字段] int IDENTITY (1,1) PRIMARY KEY ,[字段1] nVarChar(50) default ...

  5. mysql中用命令行复制表结构(数据)

    mysql中用命令行复制表结构的方法主要有一下几种: 1.只复制表结构到新表 CREATE TABLE 新表 SELECT * FROM 旧表 WHERE 1=2; 或 CREATE TABLE 新表 ...

  6. SQL Server 修改表结构(转载)

    SQL Server 修改表结构 本文链接:https://blog.csdn.net/petezh/article/details/81744374 查看指定表结构 exec sp_help Rep ...

  7. MySQL中使用sql语句获得表结构

    最近在研究PHP,那么就必须涉及到mysql.其中一个功能通过表数据自动生成页面,紧接着发现在一张空表中无法读取数据(因为人家刚刚新建,就是空的没有数据) 延伸出来便是直接查表结构获得字段名,再进行处 ...

  8. SQL server学习(二)表结构操作、SQL函数、高级查询

    数据库查询的基本格式为: select ----输出(显示)你要查询出来的值 from -----查询的依据 where -----筛选条件(对依据(数据库中存在的表)) group by ----- ...

  9. 【转】Mysql学习---SQL的优化

    [原文]https://www.toutiao.com/i6594314336913588743/ mysql如何处理亿级数据,第一个阶段--优化SQL语句 1.应尽量避免在 where 子句中使用! ...

随机推荐

  1. java NIO学前准备

    之前一直对NIO感兴趣,无奈对IO的很多概念很模糊,所以对于NIO的学习也是一直半解,最近在网上查阅了很多资料,发现有很多概念是需要反复理解的,有的时候甚至当时理解了,但一段时间后又忘记了,所以决定自 ...

  2. LINUX安装UNZIP

    安装完linux ,发现没有UNZIP,没办法,重新安装. 1.获取unzip源码 sudo wget http://downloads.sourceforge.net/infozip/unzip55 ...

  3. EntityFrameWork Code First 一对多关系处理

    场景1: 一个文章类别(Category)下含有多篇文章(Article),而某篇文章只能对应一个类别 Article和Category的代码如下: /// <summary> /// 文 ...

  4. ubuntu下安装Sublime Text2

    1.下载Sublime Text2官网下载地址:http://www.sublimetext.com 2.安装Sublime Text2解压即可使用,如linjiqin@ubuntu:~$ sudo ...

  5. Java ArrayList trimToSize()

    前几天看了Java ArrayList,没有明白trimToSize()这个方法是什么意思,所以看了一下源码并且debug一下自己的一个例子,明白了其中的含义.贴在这里. ArrayList al = ...

  6. 栈C++实现

    栈的核心是LIFO(Last In First Out),即后进先出 出栈和入栈只会对栈顶进行操作,栈底永远为0.如果是入栈,要将入栈元素赋值给栈数组,再将栈顶上移一位:出栈时要先将栈顶下移一位,再将 ...

  7. yum无法正常安装,提示如下 There are no enabled repos Run "yum repolist all"

    一般来说著名的linux系统基本上分两大类:1 RedHat系列:Redhat.Centos.Fedora等2 Debian系列:Debian.Ubuntu等RedHat 系列:1 常见的安装包格式 ...

  8. MySQL 8.0 压缩包版安装方法

    转自:https://blog.csdn.net/yangs_2012/article/details/80412016 注意: 操作系统:Windows 10 专业版(64位) MySQL版本:my ...

  9. IntelliJ IDEA 快捷键(一)(window版)

    一.高效定位代码 1.跳转 1.项目之间的跳转 Next Project Window 快捷键 Ctrl + Alt + 左方括号. Previous Project Window 快捷键 Ctrl ...

  10. Redis(什么是Redis?)

    Redis是一个开源的内存数据库,可以作为缓存也可以作为消息队列.它支持的数据结构有:字符串.哈希表.列表.集合.有序集合. Redis:Redis是Remote Dictionary Server( ...