新建一個叫做 review 的數據庫,將測試數據腳本導進去。(可以使用Navicat查詢功能)

/* Navicat MySQL Data Transfer Source Server : DB Source Server Version : 50723 Source Host : localhost:3306 Source Database : review Target Server Type : MYSQL Target Server Version : 50723 File Encoding : 65001 Date: 2019-02-25 23:48:25 */ SET FOREIGN_KEY_CHECKS=0; -- ---------------------------- -- Table structure for class -- ---------------------------- DROP TABLE IF EXISTS `class`; CREATE TABLE `class` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` char(4) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8; -- ---------------------------- -- Records of class -- ---------------------------- INSERT INTO `class` VALUES ('1', '高三1班'); INSERT INTO `class` VALUES ('2', '高三2班'); INSERT INTO `class` VALUES ('3', '高三3班'); -- ---------------------------- -- Table structure for course -- ---------------------------- DROP TABLE IF EXISTS `course`; CREATE TABLE `course` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` char(8) NOT NULL, `teacher_id` int(11) DEFAULT NULL, PRIMARY KEY (`id`), UNIQUE KEY `name` (`name`), KEY `fk_cou_tea` (`teacher_id`), CONSTRAINT `fk_cou_tea` FOREIGN KEY (`teacher_id`) REFERENCES `teacher` (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8; -- ---------------------------- -- Records of course -- ---------------------------- INSERT INTO `course` VALUES ('1', 'python', '1'); INSERT INTO `course` VALUES ('2', 'java', '2'); INSERT INTO `course` VALUES ('3', 'php', '3'); INSERT INTO `course` VALUES ('4', 'c', '1'); -- ---------------------------- -- Table structure for score -- ---------------------------- DROP TABLE IF EXISTS `score`; CREATE TABLE `score` ( `id` int(11) NOT NULL AUTO_INCREMENT, `student_id` int(11) DEFAULT NULL, `course_id` int(11) DEFAULT NULL, `mark` int(11) DEFAULT NULL, PRIMARY KEY (`id`), KEY `fk_sco_stu` (`student_id`), KEY `fk_sco_cou` (`course_id`), CONSTRAINT `fk_sco_cou` FOREIGN KEY (`course_id`) REFERENCES `course` (`id`), CONSTRAINT `fk_sco_stu` FOREIGN KEY (`student_id`) REFERENCES `student` (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=18 DEFAULT CHARSET=utf8; -- ---------------------------- -- Records of score -- ---------------------------- INSERT INTO `score` VALUES ('1', '1', '2', '79'); INSERT INTO `score` VALUES ('2', '2', '1', '58'); INSERT INTO `score` VALUES ('3', '2', '3', '66'); INSERT INTO `score` VALUES ('4', '2', '4', '80'); INSERT INTO `score` VALUES ('5', '3', '1', '63'); INSERT INTO `score` VALUES ('6', '3', '4', '95'); INSERT INTO `score` VALUES ('7', '4', '2', '88'); INSERT INTO `score` VALUES ('8', '4', '3', '62'); INSERT INTO `score` VALUES ('9', '5', '2', '59'); INSERT INTO `score` VALUES ('10', '5', '4', '100'); INSERT INTO `score` VALUES ('11', '1', '1', '55'); INSERT INTO `score` VALUES ('12', '3', '2', '81'); INSERT INTO `score` VALUES ('13', '4', '4', '50'); INSERT INTO `score` VALUES ('14', '5', '3', '77'); INSERT INTO `score` VALUES ('15', '1', '4', '58'); INSERT INTO `score` VALUES ('16', '1', '3', '91'); INSERT INTO `score` VALUES ('17', '6', '2', '75'); -- ---------------------------- -- Table structure for student -- ---------------------------- DROP TABLE IF EXISTS `student`; CREATE TABLE `student` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` char(2) NOT NULL, `gender` char(1) DEFAULT NULL, `class_id` int(11) DEFAULT NULL, PRIMARY KEY (`id`), KEY `fk_cou_cla` (`class_id`), CONSTRAINT `fk_cou_cla` FOREIGN KEY (`class_id`) REFERENCES `class` (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8; -- ---------------------------- -- Records of student -- ---------------------------- INSERT INTO `student` VALUES ('1', '德瑪', '男', '1'); INSERT INTO `student` VALUES ('2', '妖姬', '女', '2'); INSERT INTO `student` VALUES ('3', '盲僧', '男', '3'); INSERT INTO `student` VALUES ('4', '蜘蛛', '女', '1'); INSERT INTO `student` VALUES ('5', '卡牌', '男', '2'); INSERT INTO `student` VALUES ('6', '露露', '女', '3'); -- ---------------------------- -- Table structure for teacher -- ---------------------------- DROP TABLE IF EXISTS `teacher`; CREATE TABLE `teacher` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` char(2) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8; -- ---------------------------- -- Records of teacher -- ---------------------------- INSERT INTO `teacher` VALUES ('1', '佩奇'); INSERT INTO `teacher` VALUES ('2', '大熊'); INSERT INTO `teacher` VALUES ('3', '路飛');
數據表結構如下圖:
練習題及答案如下:
-- 1、查詢所有的課程的名稱以及對應的任課老師姓名 select course.name as "課程",teacher.name as "任課老師" from course left join teacher on course.teacher_id=teacher.id; ------------------------------------------------------------ -- 2、查詢學習課程"python"比課程"java"成績低的學生的學號 select python.student_id from (select score.student_id,course.name,score.mark from score inner join course on score.course_id=course.id where course.name="python") as python inner JOIN (select score.student_id,course.name,score.mark from score inner join course on score.course_id=course.id where course.name="java") as java on python.student_id=java.student_id where python.mark<java.mark; ------------------------------------------------------------ -- 3、查詢平均成績大於65分的同學的id和平均成績(保留兩位小數) select student_id,round(avg(mark),2) as m from score group by student_id having m>65; ------------------------------------------------------------ -- 4、查詢平均成績大於65分的同學的姓名和平均成績(保留兩位小數); select student.name,round(avg(mark),2) as m from score inner join student on score.student_id=student.id group by student_id having m>65; ------------------------------------------------------------ -- 5、查詢所有同學的姓名、選課數、總成績 select student.name,count(score.course_id) as "選課數",sum(score.mark) as "總成績" from score inner join student on score.student_id=student.id group by student_id; ------------------------------------------------------------ -- 6、查詢沒學過"路飛"老師課的同學的姓名 # 1)"路飛"老師任課的課程id select course.id from course inner join teacher on course.teacher_id=teacher.id where teacher.name="路飛" # 2)學過"路飛"老師的課的學生id select score.student_id from score where score.course_id in (select course.id from course inner join teacher on course.teacher_id=teacher.id where teacher.name="路飛") group by score.student_id # 3)最終結果 select student.name from student where id not in (select score.student_id from score where score.course_id in (select course.id from course inner join teacher on course.teacher_id=teacher.id where teacher.name="路飛") group by score.student_id); ------------------------------------------------------------ -- 7、查詢學過"python"並且也學過"java"課程的同學的姓名 select student.name from score left join student on score.student_id=student.id where score.course_id=(select id from course where name="python") or score.course_id=(select id from course where name="java") group by score.student_id having count(1)>1; ------------------------------------------------------------ -- 8、查詢學過"路飛"老師所教的全部課程的同學的姓名 select student.name from score inner join student on score.student_id=student.id where score.course_id in (select course.id from course inner join teacher on course.teacher_id=teacher.id where teacher.name="路飛") group by score.student_id; -- 9、查詢有課程成績小於60分的同學的姓名 select name from student where id in (select student_id from score where mark<60 group by student_id); ------------------------------------------------------------ -- 10、查詢掛科超過兩門(包括兩門)的學生姓名 select name from student where id in (select student_id from score where mark<60 group by student_id having count(1)>=2); ------------------------------------------------------------ -- 11、查詢選修了全部課程的學生姓名 select name from student where id in (select student_id from score group by student_id having count(1)=(select count(1) from course)); ------------------------------------------------------------ -- 12、查詢至少有一門課程與"卡牌"同學所學課程相同的同學姓名 select name from student where id in (select student_id from score where course_id in (select course_id from score inner join student on score.student_id=student.id where student.name="卡牌") group by student_id) and name!="卡牌"; ------------------------------------------------------------ -- 13、查詢學過"蜘蛛"同學全部課程的其他同學姓名 # 1)"蜘蛛"同學學過的課程id select course_id from score inner join student on score.student_id=student.id where student.name="蜘蛛" # 2)score表連student表,並篩選出課程id在("蜘蛛"同學學過的課程id),並且學生!="蜘蛛" select student.name from score inner join student on score.student_id=student.id where score.course_id in (select course_id from score inner join student on score.student_id=student.id where student.name="蜘蛛") and student.name!="蜘蛛" # 3)接着對結果進行分組,再次篩選得到最終結果 select student.name from score inner join student on score.student_id=student.id where score.course_id in (select course_id from score inner join student on score.student_id=student.id where student.name="蜘蛛") and student.name!="蜘蛛" group by student_id having count(1)=(select count(1) from score inner join student on score.student_id=student.id where student.name="蜘蛛"); ------------------------------------------------------------ -- 14、查詢和"蜘蛛"同學學習的課程完全相同的其他同學姓名; # 1)找出與"蜘蛛"學習課程數相同的學生id(你學兩門,我也學兩門) select * from score where score.student_id in (select student_id from score group by score.student_id having count(1)=(select count(1) from score inner join student on score.student_id=student.id where student.name="蜘蛛")) # 2)找出學過"蜘蛛"課程的學生,剩下的一定是至少學過一門"蜘蛛"課程的學生 select * from score where score.student_id in (select student_id from score group by score.student_id having count(1)=(select count(1) from score inner join student on score.student_id=student.id where student.name="蜘蛛")) and score.course_id in (select course_id from score inner join student on score.student_id=student.id where student.name="蜘蛛") # 3)根據學生id進行分組,剩下學生數count(1)=貂蟬學生所學課程數 select student_id from score where score.student_id in (select student_id from score group by score.student_id having count(1)=(select count(1) from score inner join student on score.student_id=student.id where student.name="蜘蛛")) and score.course_id in (select course_id from score inner join student on score.student_id=student.id where student.name="蜘蛛") group by score.student_id having count(1)=(select count(1) from score inner join student on score.student_id=student.id where student.name="蜘蛛") and score.student_id!=(select id from student where name="蜘蛛") # 4)最終結果 select name from student where id in (select student_id from score where score.student_id in (select student_id from score group by score.student_id having count(1)=(select count(1) from score inner join student on score.student_id=student.id where student.name="蜘蛛")) and score.course_id in (select course_id from score inner join student on score.student_id=student.id where student.name="蜘蛛") group by score.student_id having count(1)=(select count(1) from score inner join student on score.student_id=student.id where student.name="蜘蛛") and score.student_id!=(select id from student where name="蜘蛛")); ------------------------------------------------------------ -- 15、按平均成績倒序顯示所有學生的"python"、"java"、"php"三門的課程成績,按如下形式顯示: 學生id,python,java,php,課程數,平均分 # 1)先查詢單一學生的python課程分數 select mark from score left join course on score.course_id = course.id where course.name = "python" and score.student_id=1; # 2)將上面查詢的結果作為列字段使用,得到最終結果 select student_id, (select mark from score left join course on score.course_id = course.id where course.name = "python" and score.student_id=sc.student_id) as "python", (select mark from score left join course on score.course_id = course.id where course.name = "java" and score.student_id=sc.student_id) as "java", (select mark from score left join course on score.course_id = course.id where course.name = "php" and score.student_id=sc.student_id) as "php", count(course_id) as "課程數", avg(mark) as "平均分" from score as sc group by student_id order by avg(mark) desc; ------------------------------------------------------------ -- 16、查詢各科成績最高和最低的分:以如下形式顯示:課程id,最高分,最低分 select course_id,max(mark) as "最高分",min(mark) as "最低分" from score group by course_id; ------------------------------------------------------------ -- 17、統計各科各分數段人數,顯示格式:課程id,課程名稱,[100-85],[85-70],[70-60],[<60] select course_id,course.name, sum(case when mark between 85 and 100 then 1 else 0 end) as "[100-85]", sum(case when mark between 70 and 85 then 1 else 0 end) as "[85-70]", sum(case when mark between 60 and 70 then 1 else 0 end) as "[70-60]", sum(case when mark < 60 then 1 else 0 end) as "[<60]" from score inner join course on score.course_id=course.id GROUP BY score.course_id; ------------------------------------------------------------ -- 18、查詢每門課程名字及其被選修的次數 select course.name,count(1) from score inner join course on score.course_id=course.id group by course_id; ------------------------------------------------------------ -- 19、查詢只選修了一門課程的學生的學號和姓名 select student_id,student.name from score inner join student on score.student_id=student.id group by student_id having count(course_id)=1; ------------------------------------------------------------ -- 20、查詢學生表中男生、女生各有多少人 select sum(case when gender="男" then 1 else 0 end) as "男生", sum(case when gender="女" then 1 else 0 end) as "女生" from student ------------------------------------------------------------ -- 21、查詢每門課程的平均成績,結果按平均成績升序排列,平均成績相同時,按課程id降序排列 select course.name,avg(mark) from score inner join course on score.course_id=course.id group by course_id order by avg(mark) asc,course_id desc; ------------------------------------------------------------ -- 22、查詢課程名稱為"python"且分數低於60的學生姓名和分數 select student.name,mark from score inner join student on score.student_id=student.id where mark<60 and course_id=(select id from course where name="python"); ------------------------------------------------------------ -- 23、求選了課程的學生人數 # 方式一: select count(distinct student_id) from score; # 方式二: select count(1) from (select count(student_id) from score group by student_id) as a;