day59

参考:http://www.cnblogs.com/wupeiqi/p/5748496.html

现有数据库

  1. /*
  2. Navicat Premium Data Transfer
  3.  
  4. Source Server : localhost
  5. Source Server Type : MySQL
  6. Source Server Version : 50624
  7. Source Host : localhost
  8. Source Database : sqlexam
  9.  
  10. Target Server Type : MySQL
  11. Target Server Version : 50624
  12. File Encoding : utf-8
  13.  
  14. Date: 10/21/2016 06:46:46 AM
  15. */
  16.  
  17. SET NAMES utf8;
  18. SET FOREIGN_KEY_CHECKS = 0;
  19.  
  20. -- ----------------------------
  21. -- Table structure for `class`
  22. -- ----------------------------
  23. DROP TABLE IF EXISTS `class`;
  24. CREATE TABLE `class` (
  25. `cid` int(11) NOT NULL AUTO_INCREMENT,
  26. `caption` varchar(32) NOT NULL,
  27. PRIMARY KEY (`cid`)
  28. ) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8;
  29.  
  30. -- ----------------------------
  31. -- Records of `class`
  32. -- ----------------------------
  33. BEGIN;
  34. INSERT INTO `class` VALUES ('', '三年二班'), ('', '三年三班'), ('', '一年二班'), ('', '二年九班');
  35. COMMIT;
  36.  
  37. -- ----------------------------
  38. -- Table structure for `course`
  39. -- ----------------------------
  40. DROP TABLE IF EXISTS `course`;
  41. CREATE TABLE `course` (
  42. `cid` int(11) NOT NULL AUTO_INCREMENT,
  43. `cname` varchar(32) NOT NULL,
  44. `teacher_id` int(11) NOT NULL,
  45. PRIMARY KEY (`cid`),
  46. KEY `fk_course_teacher` (`teacher_id`),
  47. CONSTRAINT `fk_course_teacher` FOREIGN KEY (`teacher_id`) REFERENCES `teacher` (`tid`)
  48. ) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8;
  49.  
  50. -- ----------------------------
  51. -- Records of `course`
  52. -- ----------------------------
  53. BEGIN;
  54. INSERT INTO `course` VALUES ('', '生物', ''), ('', '物理', ''), ('', '体育', ''), ('', '美术', '');
  55. COMMIT;
  56.  
  57. -- ----------------------------
  58. -- Table structure for `score`
  59. -- ----------------------------
  60. DROP TABLE IF EXISTS `score`;
  61. CREATE TABLE `score` (
  62. `sid` int(11) NOT NULL AUTO_INCREMENT,
  63. `student_id` int(11) NOT NULL,
  64. `course_id` int(11) NOT NULL,
  65. `num` int(11) NOT NULL,
  66. PRIMARY KEY (`sid`),
  67. KEY `fk_score_student` (`student_id`),
  68. KEY `fk_score_course` (`course_id`),
  69. CONSTRAINT `fk_score_course` FOREIGN KEY (`course_id`) REFERENCES `course` (`cid`),
  70. CONSTRAINT `fk_score_student` FOREIGN KEY (`student_id`) REFERENCES `student` (`sid`)
  71. ) ENGINE=InnoDB AUTO_INCREMENT=53 DEFAULT CHARSET=utf8;
  72.  
  73. -- ----------------------------
  74. -- Records of `score`
  75. -- ----------------------------
  76. BEGIN;
  77. INSERT INTO `score` VALUES ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', '');
  78. COMMIT;
  79.  
  80. -- ----------------------------
  81. -- Table structure for `student`
  82. -- ----------------------------
  83. DROP TABLE IF EXISTS `student`;
  84. CREATE TABLE `student` (
  85. `sid` int(11) NOT NULL AUTO_INCREMENT,
  86. `gender` char(1) NOT NULL,
  87. `class_id` int(11) NOT NULL,
  88. `sname` varchar(32) NOT NULL,
  89. PRIMARY KEY (`sid`),
  90. KEY `fk_class` (`class_id`),
  91. CONSTRAINT `fk_class` FOREIGN KEY (`class_id`) REFERENCES `class` (`cid`)
  92. ) ENGINE=InnoDB AUTO_INCREMENT=17 DEFAULT CHARSET=utf8;
  93.  
  94. -- ----------------------------
  95. -- Records of `student`
  96. -- ----------------------------
  97. BEGIN;
  98. INSERT INTO `student` VALUES ('', '男', '', '理解'), ('', '女', '', '钢蛋'), ('', '男', '', '张三'), ('', '男', '', '张一'), ('', '女', '', '张二'), ('', '男', '', '张四'), ('', '女', '', '铁锤'), ('', '男', '', '李三'), ('', '男', '', '李一'), ('', '女', '', '李二'), ('', '男', '', '李四'), ('', '女', '', '如花'), ('', '男', '', '刘三'), ('', '男', '', '刘一'), ('', '女', '', '刘二'), ('', '男', '', '刘四');
  99. COMMIT;
  100.  
  101. -- ----------------------------
  102. -- Table structure for `teacher`
  103. -- ----------------------------
  104. DROP TABLE IF EXISTS `teacher`;
  105. CREATE TABLE `teacher` (
  106. `tid` int(11) NOT NULL AUTO_INCREMENT,
  107. `tname` varchar(32) NOT NULL,
  108. PRIMARY KEY (`tid`)
  109. ) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8;
  110.  
  111. -- ----------------------------
  112. -- Records of `teacher`
  113. -- ----------------------------
  114. BEGIN;
  115. INSERT INTO `teacher` VALUES ('', '张磊老师'), ('', '李平老师'), ('', '刘海燕老师'), ('', '朱云海老师'), ('', '李杰老师');
  116. COMMIT;
  117.  
  118. SET FOREIGN_KEY_CHECKS = 1;
  119.  
  120. 表结构和数据

题目与答案:

第2题

  1. #2、查询“生物”课程比“物理”课程成绩高的所有学生的学号;
  2.  
  3. #先连表 课程和成绩
  4. select * from score left join course on score.course_id = course.cid;
  5.  
  6. #选出生物的
  7. select * from score left join course on score.course_id = course.cid where course.cname = "生物";
  8.  
  9. #从中选出课程id,学生id,课程及分数
  10. select score.sid, score.student_id, course.cname,score.num from score left join course on score.course_id = course.cid where course.cname = "生物";
  11.  
  12. select score.sid, score.student_id, course.cname,score.num from score left join course on score.course_id = course.cid where course.cname = "物理";
  13.  
  14. #只能左右比较,所以 根据学生id左右连接 ,将生物与物理连接
  15. select * from
  16. (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
  17. inner join #存在空不显示,
  18. (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
  19. on A.student_id = B.student_id;
  20.  
  21. #根据条件进行选择
  22. select * from
  23. (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
  24. inner join #存在空不显示,
  25. (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
  26. on A.student_id = B.student_id
  27. where A.num > B.num;
  28.  
  29. #只要学号
  30. select A.student_id from
  31. (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
  32. inner join #存在空不显示,
  33. (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
  34. on A.student_id = B.student_id
  35. where A.num > B.num;
  36.  
  37. -- #选出有生物和物理的信息
  38. -- select * from score left join course on score.course_id = course.cid where course.cname = "生物" or course.cname = "物理";
  39. --
  40. -- #从中选出课程id,学生id,课程及分数
  41. -- 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 = "物理";
  42. --

第5题

  1. #查询姓李的老师的个数
  2. select count(tid) from teacher where tname like '李%'

第6题

  1. #6、查询没学过“李平”老师课的同学的学号、姓名;
  2.  
  3. #先连接teacher和course
  4. select * from course left join teacher on course.teacher_id = teacher.tid;
  5.  
  6. #挑出李平老师的课
  7. select * from course left join teacher on course.teacher_id = teacher.tid where teacher.tname = '李平老师';
  8.  
  9. #选出其中的李平课程的id 2,4
  10. select course.cid from course left join teacher on course.teacher_id = teacher.tid where teacher.tname = '李平老师';
  11.  
  12. #选择选过李平课的学生
  13. select * from score where course_id in
  14. (select course.cid from course left join teacher on course.teacher_id = teacher.tid where teacher.tname = '李平老师');
  15.  
  16. #选过李平课的学生ID
  17. select student_id from score where course_id in
  18. (select course.cid from course left join teacher on course.teacher_id = teacher.tid where teacher.tname = '李平老师')
  19. group by student_id;
  20.  
  21. #到学生表中排除出以上ID
  22. select * from student where sid not in
  23. (
  24. select student_id from score where course_id in
  25. (select course.cid from course left join teacher on course.teacher_id = teacher.tid where teacher.tname = '李平老师')
  26. group by student_id
  27. );
  28.  
  29. #最终选出学号和姓名
  30. select student.sid, student.sname from student where sid not in
  31. (
  32. select student_id from score where course_id in
  33. (select course.cid from course left join teacher on course.teacher_id = teacher.tid where teacher.tname = '李平老师')
  34. group by student_id
  35. );

第7题

  1. #7、查询学过“001”课程并且也学过编号“002”课程的同学的学号、姓名;
  2.  
  3. #选出学过 1和2 课程的学生信息
  4. select * from score where course_id = 1 or course_id = 2 ;
  5.  
  6. #根据 学号进行分组
  7. 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门课的说明是12都选了
  8.  
  9. #学生id已知, 匹配student名字
  10. select student.sid,student.sname from
  11. (select student_id from score where course_id = 1 or course_id = 2 GROUP BY student_id having count(course_id)>1) as A
  12. left join student on A.student_id = student.sid;

第8题

  1. #8、查询学过“李平”老师所教的所有课的同学的学号、姓名;
  2.  
  3. #先拿“李平"课程id 老师和课程先连表
  4. select * from course left join teacher on teacher.tid = course.teacher_id;
  5.  
  6. #拿出课程id
  7. select course.cid from course left join teacher on teacher.tid = course.teacher_id where teacher.tname = "李平老师";
  8. #2,4
  9.  
  10. #根据2,4从score中选出学生信息
  11. select * from score where course_id in
  12. (select course.cid from course left join teacher on teacher.tid = course.teacher_id where teacher.tname = "李平老师");
  13. #其中这些学生都选了李平的课
  14.  
  15. #选出李平的课都上的学生
  16. select student_id from score where course_id in
  17. (select course.cid from course left join teacher on teacher.tid = course.teacher_id where teacher.tname = "李平老师")
  18. 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 = "李平老师");
  19. #等于李老师的课程数量即为上了老师的所有课

第10题

  1. #10、查询有课程成绩小于60分的同学的学号、姓名;
  2.  
  3. #找到有成绩小于60的学生
  4. select * from score where num < 60;
  5.  
  6. #找到有成绩小于60的学生的id
  7. select student_id from score where num < 60;
  8.  
  9. #并根据学号分组,因为有重复id出现,挂了多科的
  10. select student_id from score where num < 60 GROUP BY student_id;
  11. select distinct student_id from score where num < 60;#去重第二个方法
  12.  
  13. #和student进行连表
  14. 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;
  15.  
  16. #第二个方法
  17. select sid,sname from student where sid in (
  18. select distinct student_id from score where num < 60
  19. )

第11题

  1. #11、查询没有学全所有课的同学的学号、姓名;
  2.  
  3. #获得课程总数
  4. select count(cname) from course;
  5. #select count(cid) from course;#主键速度更快
  6.  
  7. #每个学生id的上的课数, 如果不写 group by student_id会报错因为是根据学生id分组的
  8. select student_id, count(course_id) as course_num from score group by student_id;
  9.  
  10. #找出未选满的学生id
  11. select student_id from
  12. (select student_id, count(course_id) as course_num from score group by student_id) as A
  13. where A.course_num not in (select count(cname) from course);
  14.  
  15. #对应名字
  16. select sid,sname from student where sid in
  17. (
  18. select student_id from
  19. (select student_id, count(course_id) as course_num from score group by student_id) as A
  20. where A.course_num not in (select count(cname) from course)
  21. );

第12题

  1. #12、查询 至少有一门课与学号为“001”的同学所学相同的同学的学号和姓名;
  2. #即和001一起上过课的
  3.  
  4. #选出了课程001号同学的课程id
  5. select course_id from score where student_id = 1;
  6.  
  7. #选出和001号一起学习的ID
  8. select DISTINCT student_id from score where course_id in (select course_id from score where student_id = 1);
  9.  
  10. 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;
  11. #还需排除自身

MySQL(作业练习)的更多相关文章

  1. Mysql 作业(Scheduler)

    200 ? "200px" : this.width)!important;} --> 介绍 作业也叫做事件调度,其实它也就是一个时间触发器:它可以定义某个时间点执行指定的数 ...

  2. mysql定时任务/mysql作业

    转自:https://www.jb51.net/article/138569.htm 详细参考:https://www.cnblogs.com/qlqwjy/p/7954175.html(事件& ...

  3. MySQL作业

    创建作业事件 MONTH STARTS '2015-01-01 05:30:01' ON COMPLETION NOT PRESERVE ENABLE DO CALL sp_moveLoginReco ...

  4. mysql安装及基本操作(mysql作业)

    1 官网下载,链接  https://www.mysql.com/downloads/ Download MySQL Community Server 默认为你选好了Mac OS X 平台 选择的是. ...

  5. 【MySQL作业】MySQL函数——美和易思系统信息函数和加密函数应用习题

    点击打开所使用到的数据库>>> 1.显示当前 MySQL 服务器的版本信息和登录信息. MySQL 系统信息函数 version() 用于返回当前 MySQL 的版本号," ...

  6. 【MySQL作业】MySQL函数——美和易思日期和时间函数应用习题

    点击打开所使用到的数据库>>> 1.采用尽可能多的方式显示当前系统日期和时间. 下列 SQL 语句可以显示当前系统的日期和时间: curdate() 和 current_date() ...

  7. 【MySQL作业】MySQL函数——美和易思字符串函数应用习题

    点击打开所使用到的数据库>>> 1.将所有客户的姓名与电话以"-"作为分隔符进行连接显示. 使用 concat(s1,s2,-) 函数将所有客户的姓名与电话以&q ...

  8. 【MySQL作业】MySQL函数——美和易思数学函数和控制流函数应用习题

    点击打开所使用到的数据库>>> 1.添加一条商品记录.  商品编码  goodsCode 商品名 goodsName 种类 category 单价 unitPrice 02005 夏 ...

  9. 【MySQL作业】DDL 和 DML——美和易思使用 DML 删除表数据应用习题

    点击打开所使用到的数据库>>> 删除客户"刘一鸣". 执行 SQL 代码"delete from customer where cName=' 刘一鸣 ...

随机推荐

  1. 用AI制作炫酷效果

    PART1:制作第一个效果 步骤一:新建一个800*600的画布. 骤二:从工具栏选“矩形工具”,创建一个800*600的矩形.白色的是画布,浅红色(我的AI之前保留的填充颜色,每个人都不一样)的是你 ...

  2. Debian use sudo

    刚安装好的Debian默认还没有sudo功能.1.安装sudo# apt-get install sudo2.编辑 /etc/sudoers ,添加如下行# visudoroot ALL=(ALL:A ...

  3. Socket-IO 系列(三)基于 NIO 的同步非阻塞式编程

    Socket-IO 系列(三)基于 NIO 的同步非阻塞式编程 缓冲区(Buffer) 用于存储数据 通道(Channel) 用于传输数据 多路复用器(Selector) 用于轮询 Channel 状 ...

  4. Kendo UI中TreeView 放入tabstrip中,大数据量时超过边框的解决方案。

    参考http://www.kendoui.com/forums/ui/tabstrip/tabstip-with-treeview-treeview-breaking-out-of-tabstrip. ...

  5. 【Web】网站主如何更改网页标签的图标(favicon.ico)

    修改web项目的favicon图标,方式有两种:全局方式和局部方式 全局方式: 进入服务器\webapps\ROOT,然后用自己的favicon.ico替换服务器自带的favicon.ico图片 局部 ...

  6. 2018.07.22 洛谷P4316 绿豆蛙的归宿(概率dp)

    传送门 简单的递推. 由于是DAG" role="presentation" style="position: relative;">DAGDA ...

  7. 32. My Experiences in the Factories 我在工厂的经历

    32. My Experiences in the Factories 我在工厂的经历 ① I've worked in the factories surrounding my hometown e ...

  8. 22. Valuing Water 珍惜水资源

    . Valuing Water 珍惜水资源 ① Humanity uses a little less than half the water available worldwide.Yet occu ...

  9. 打开子页面及刷新父页面 window.open window.opener.reload()

    //打开子页面 var url=children_url;window.open(url) //刷新parent页面 var url=parent_urlfunction refresh(url){  ...

  10. 基于图像切换器(imageSwitcher)的支持动画的图片浏览器

    利用GridView和ImageSwitcher的基本用法 public class MainActivity extends Activity { int[] imageIds = new int[ ...