标签:student alt 建表 bsp efault str family char navicat
创建表结果和数据准备[直接执行即可]
1 /* 2 Navicat MySQL Data Transfer 3 4 Source Server : ftl1012 5 Source Server Version : 50617 6 Source Host : localhost:3306 7 Source Database : test_python 8 9 Target Server Type : MYSQL 10 Target Server Version : 50617 11 File Encoding : 65001 12 13 Date: 2017-12-30 13:12:57 14 */ 15 16 SET FOREIGN_KEY_CHECKS=0; 17 18 -- ---------------------------- 19 -- Table structure for class 20 -- ---------------------------- 21 DROP TABLE IF EXISTS `class`; 22 CREATE TABLE `class` ( 23 `cid` int(11) NOT NULL AUTO_INCREMENT, 24 `caption` varchar(32) NOT NULL, 25 PRIMARY KEY (`cid`) 26 ) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8; 27 28 -- ---------------------------- 29 -- Records of class 30 -- ---------------------------- 31 INSERT INTO `class` VALUES (‘1‘, ‘三年二班‘); 32 INSERT INTO `class` VALUES (‘2‘, ‘三年三班‘); 33 INSERT INTO `class` VALUES (‘3‘, ‘一年二班‘); 34 INSERT INTO `class` VALUES (‘4‘, ‘二年九班‘); 35 36 -- ---------------------------- 37 -- Table structure for course 38 -- ---------------------------- 39 DROP TABLE IF EXISTS `course`; 40 CREATE TABLE `course` ( 41 `cid` int(11) NOT NULL AUTO_INCREMENT, 42 `cname` varchar(32) NOT NULL, 43 `teacher_id` int(11) NOT NULL, 44 PRIMARY KEY (`cid`), 45 KEY `fk_course_teacher` (`teacher_id`), 46 CONSTRAINT `fk_course_teacher` FOREIGN KEY (`teacher_id`) REFERENCES `teacher` (`tid`) 47 ) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8; 48 49 -- ---------------------------- 50 -- Records of course 51 -- ---------------------------- 52 INSERT INTO `course` VALUES (‘1‘, ‘生物‘, ‘1‘); 53 INSERT INTO `course` VALUES (‘2‘, ‘物理‘, ‘2‘); 54 INSERT INTO `course` VALUES (‘3‘, ‘体育‘, ‘3‘); 55 INSERT INTO `course` VALUES (‘4‘, ‘美术‘, ‘2‘); 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 INSERT INTO `score` VALUES (‘1‘, ‘1‘, ‘1‘, ‘10‘); 77 INSERT INTO `score` VALUES (‘2‘, ‘1‘, ‘2‘, ‘9‘); 78 INSERT INTO `score` VALUES (‘5‘, ‘1‘, ‘4‘, ‘66‘); 79 INSERT INTO `score` VALUES (‘6‘, ‘2‘, ‘1‘, ‘8‘); 80 INSERT INTO `score` VALUES (‘8‘, ‘2‘, ‘3‘, ‘68‘); 81 INSERT INTO `score` VALUES (‘9‘, ‘2‘, ‘4‘, ‘99‘); 82 INSERT INTO `score` VALUES (‘10‘, ‘3‘, ‘1‘, ‘77‘); 83 INSERT INTO `score` VALUES (‘11‘, ‘3‘, ‘2‘, ‘66‘); 84 INSERT INTO `score` VALUES (‘12‘, ‘3‘, ‘3‘, ‘87‘); 85 INSERT INTO `score` VALUES (‘13‘, ‘3‘, ‘4‘, ‘99‘); 86 INSERT INTO `score` VALUES (‘14‘, ‘4‘, ‘1‘, ‘79‘); 87 INSERT INTO `score` VALUES (‘15‘, ‘4‘, ‘2‘, ‘11‘); 88 INSERT INTO `score` VALUES (‘16‘, ‘4‘, ‘3‘, ‘67‘); 89 INSERT INTO `score` VALUES (‘17‘, ‘4‘, ‘4‘, ‘100‘); 90 INSERT INTO `score` VALUES (‘18‘, ‘5‘, ‘1‘, ‘79‘); 91 INSERT INTO `score` VALUES (‘19‘, ‘5‘, ‘2‘, ‘11‘); 92 INSERT INTO `score` VALUES (‘20‘, ‘5‘, ‘3‘, ‘67‘); 93 INSERT INTO `score` VALUES (‘21‘, ‘5‘, ‘4‘, ‘100‘); 94 INSERT INTO `score` VALUES (‘22‘, ‘6‘, ‘1‘, ‘9‘); 95 INSERT INTO `score` VALUES (‘23‘, ‘6‘, ‘2‘, ‘100‘); 96 INSERT INTO `score` VALUES (‘24‘, ‘6‘, ‘3‘, ‘67‘); 97 INSERT INTO `score` VALUES (‘25‘, ‘6‘, ‘4‘, ‘100‘); 98 INSERT INTO `score` VALUES (‘26‘, ‘7‘, ‘1‘, ‘9‘); 99 INSERT INTO `score` VALUES (‘27‘, ‘7‘, ‘2‘, ‘100‘); 100 INSERT INTO `score` VALUES (‘28‘, ‘7‘, ‘3‘, ‘67‘); 101 INSERT INTO `score` VALUES (‘29‘, ‘7‘, ‘4‘, ‘88‘); 102 INSERT INTO `score` VALUES (‘30‘, ‘8‘, ‘1‘, ‘9‘); 103 INSERT INTO `score` VALUES (‘31‘, ‘8‘, ‘2‘, ‘100‘); 104 INSERT INTO `score` VALUES (‘32‘, ‘8‘, ‘3‘, ‘67‘); 105 INSERT INTO `score` VALUES (‘33‘, ‘8‘, ‘4‘, ‘88‘); 106 INSERT INTO `score` VALUES (‘34‘, ‘9‘, ‘1‘, ‘91‘); 107 INSERT INTO `score` VALUES (‘35‘, ‘9‘, ‘2‘, ‘88‘); 108 INSERT INTO `score` VALUES (‘36‘, ‘9‘, ‘3‘, ‘67‘); 109 INSERT INTO `score` VALUES (‘37‘, ‘9‘, ‘4‘, ‘22‘); 110 INSERT INTO `score` VALUES (‘38‘, ‘10‘, ‘1‘, ‘90‘); 111 INSERT INTO `score` VALUES (‘39‘, ‘10‘, ‘2‘, ‘77‘); 112 INSERT INTO `score` VALUES (‘40‘, ‘10‘, ‘3‘, ‘43‘); 113 INSERT INTO `score` VALUES (‘41‘, ‘10‘, ‘4‘, ‘87‘); 114 INSERT INTO `score` VALUES (‘42‘, ‘11‘, ‘1‘, ‘90‘); 115 INSERT INTO `score` VALUES (‘43‘, ‘11‘, ‘2‘, ‘77‘); 116 INSERT INTO `score` VALUES (‘44‘, ‘11‘, ‘3‘, ‘43‘); 117 INSERT INTO `score` VALUES (‘45‘, ‘11‘, ‘4‘, ‘87‘); 118 INSERT INTO `score` VALUES (‘46‘, ‘12‘, ‘1‘, ‘90‘); 119 INSERT INTO `score` VALUES (‘47‘, ‘12‘, ‘2‘, ‘77‘); 120 INSERT INTO `score` VALUES (‘48‘, ‘12‘, ‘3‘, ‘43‘); 121 INSERT INTO `score` VALUES (‘49‘, ‘12‘, ‘4‘, ‘87‘); 122 INSERT INTO `score` VALUES (‘52‘, ‘13‘, ‘3‘, ‘87‘); 123 124 -- ---------------------------- 125 -- Table structure for student 126 -- ---------------------------- 127 DROP TABLE IF EXISTS `student`; 128 CREATE TABLE `student` ( 129 `sid` int(11) NOT NULL AUTO_INCREMENT, 130 `gender` char(1) NOT NULL, 131 `class_id` int(11) NOT NULL, 132 `sname` varchar(32) NOT NULL, 133 PRIMARY KEY (`sid`), 134 KEY `fk_class` (`class_id`), 135 CONSTRAINT `fk_class` FOREIGN KEY (`class_id`) REFERENCES `class` (`cid`) 136 ) ENGINE=InnoDB AUTO_INCREMENT=17 DEFAULT CHARSET=utf8; 137 138 -- ---------------------------- 139 -- Records of student 140 -- ---------------------------- 141 INSERT INTO `student` VALUES (‘1‘, ‘男‘, ‘1‘, ‘理解‘); 142 INSERT INTO `student` VALUES (‘2‘, ‘女‘, ‘1‘, ‘钢蛋‘); 143 INSERT INTO `student` VALUES (‘3‘, ‘男‘, ‘1‘, ‘张三‘); 144 INSERT INTO `student` VALUES (‘4‘, ‘男‘, ‘1‘, ‘张一‘); 145 INSERT INTO `student` VALUES (‘5‘, ‘女‘, ‘1‘, ‘张二‘); 146 INSERT INTO `student` VALUES (‘6‘, ‘男‘, ‘1‘, ‘张四‘); 147 INSERT INTO `student` VALUES (‘7‘, ‘女‘, ‘2‘, ‘铁锤‘); 148 INSERT INTO `student` VALUES (‘8‘, ‘男‘, ‘2‘, ‘李三‘); 149 INSERT INTO `student` VALUES (‘9‘, ‘男‘, ‘2‘, ‘李一‘); 150 INSERT INTO `student` VALUES (‘10‘, ‘女‘, ‘2‘, ‘李二‘); 151 INSERT INTO `student` VALUES (‘11‘, ‘男‘, ‘2‘, ‘李四‘); 152 INSERT INTO `student` VALUES (‘12‘, ‘女‘, ‘3‘, ‘如花‘); 153 INSERT INTO `student` VALUES (‘13‘, ‘男‘, ‘3‘, ‘刘三‘); 154 INSERT INTO `student` VALUES (‘14‘, ‘男‘, ‘3‘, ‘刘一‘); 155 INSERT INTO `student` VALUES (‘15‘, ‘女‘, ‘3‘, ‘刘二‘); 156 INSERT INTO `student` VALUES (‘16‘, ‘男‘, ‘3‘, ‘刘四‘); 157 158 -- ---------------------------- 159 -- Table structure for teacher 160 -- ---------------------------- 161 DROP TABLE IF EXISTS `teacher`; 162 CREATE TABLE `teacher` ( 163 `tid` int(11) NOT NULL AUTO_INCREMENT, 164 `tname` varchar(32) NOT NULL, 165 PRIMARY KEY (`tid`) 166 ) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8; 167 168 -- ---------------------------- 169 -- Records of teacher 170 -- ---------------------------- 171 INSERT INTO `teacher` VALUES (‘1‘, ‘张磊老师‘); 172 INSERT INTO `teacher` VALUES (‘2‘, ‘李平老师‘); 173 INSERT INTO `teacher` VALUES (‘3‘, ‘刘海燕老师‘); 174 INSERT INTO `teacher` VALUES (‘4‘, ‘朱云海老师‘); 175 INSERT INTO `teacher` VALUES (‘5‘, ‘李杰老师‘);
经验: 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
标签:student alt 建表 bsp efault str family char navicat
原文地址:https://www.cnblogs.com/ftl1012/p/9385161.html