1. 程式人生 > >MySQL(作業練習)

MySQL(作業練習)

day59

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

 現有資料庫

/*
 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 ('1', '三年二班'), ('2', '三年三班'), ('3', '一年二班'), ('4', '二年九班'); 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 ('1', '生物', '1'), ('2', '物理', '2'), ('3', '體育', '3'), ('4', '美術', '2'); 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 ('1', '1', '1', '10'), ('2', '1', '2', '9'), ('5', '1', '4', '66'), ('6', '2', '1', '8'), ('8', '2', '3', '68'), ('9', '2', '4', '99'), ('10', '3', '1', '77'), ('11', '3', '2', '66'), ('12', '3', '3', '87'), ('13', '3', '4', '99'), ('14', '4', '1', '79'), ('15', '4', '2', '11'), ('16', '4', '3', '67'), ('17', '4', '4', '100'), ('18', '5', '1', '79'), ('19', '5', '2', '11'), ('20', '5', '3', '67'), ('21', '5', '4', '100'), ('22', '6', '1', '9'), ('23', '6', '2', '100'), ('24', '6', '3', '67'), ('25', '6', '4', '100'), ('26', '7', '1', '9'), ('27', '7', '2', '100'), ('28', '7', '3', '67'), ('29', '7', '4', '88'), ('30', '8', '1', '9'), ('31', '8', '2', '100'), ('32', '8', '3', '67'), ('33', '8', '4', '88'), ('34', '9', '1', '91'), ('35', '9', '2', '88'), ('36', '9', '3', '67'), ('37', '9', '4', '22'), ('38', '10', '1', '90'), ('39', '10', '2', '77'), ('40', '10', '3', '43'), ('41', '10', '4', '87'), ('42', '11', '1', '90'), ('43', '11', '2', '77'), ('44', '11', '3', '43'), ('45', '11', '4', '87'), ('46', '12', '1', '90'), ('47', '12', '2', '77'), ('48', '12', '3', '43'), ('49', '12', '4', '87'), ('52', '13', '3', '87'); 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 ('1', '', '1', '理解'), ('2', '', '1', '鋼蛋'), ('3', '', '1', '張三'), ('4', '', '1', '張一'), ('5', '', '1', '張二'), ('6', '', '1', '張四'), ('7', '', '2', '鐵錘'), ('8', '', '2', '李三'), ('9', '', '2', '李一'), ('10', '', '2', '李二'), ('11', '', '2', '李四'), ('12', '', '3', '如花'), ('13', '', '3', '劉三'), ('14', '', '3', '劉一'), ('15', '', '3', '劉二'), ('16', '', '3', '劉四'); 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 ('1', '張磊老師'), ('2', '李平老師'), ('3', '劉海燕老師'), ('4', '朱雲海老師'), ('5', '李傑老師'); COMMIT; SET FOREIGN_KEY_CHECKS = 1; 表結構和資料

 

題目與答案:

第2題

#2、查詢“生物”課程比“物理”課程成績高的所有學生的學號;

#先連表 課程和成績
select * from score left join course on score.course_id = course.cid;

#選出生物的
select * from score left join course on score.course_id = course.cid where course.cname = "生物";

#從中選出課程id,學生id,課程及分數
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 = "物理";

#只能左右比較,所以 根據學生id左右連線 ,將生物與物理連線
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;

#根據條件進行選擇
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;

#只要學號
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;





-- #選出有生物和物理的資訊
-- select * from score left join course on score.course_id = course.cid where course.cname = "生物" or course.cname = "物理";
-- 
-- #從中選出課程id,學生id,課程及分數
-- 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 = "物理";
-- 

 

第5題

#查詢姓李的老師的個數
select count(tid) from teacher where tname like '李%'

 

第6題

#6、查詢沒學過“李平”老師課的同學的學號、姓名;

#先連線teacher和course
select * from course left join teacher on course.teacher_id = teacher.tid;

#挑出李平老師的課
select * from course left join teacher on course.teacher_id = teacher.tid where teacher.tname = '李平老師';

#選出其中的李平課程的id   24
select course.cid from course left join teacher on course.teacher_id = teacher.tid where teacher.tname = '李平老師';

#選擇選過李平課的學生
select * from score where course_id in 
(select course.cid from course left join teacher on course.teacher_id = teacher.tid where teacher.tname = '李平老師');

#選過李平課的學生ID
select student_id from score where course_id in 
(select course.cid from course left join teacher on course.teacher_id = teacher.tid where teacher.tname = '李平老師')
group by student_id;


#到學生表中排除出以上ID
select * from student where sid not in 
(
select student_id from score where course_id in 
(select course.cid from course left join teacher on course.teacher_id = teacher.tid 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 course left join teacher on course.teacher_id = teacher.tid where teacher.tname = '李平老師')
group by student_id
);

 

第7題

#7、查詢學過“001”課程並且也學過編號“002”課程的同學的學號、姓名;


#選出學過 1和2 課程的學生資訊
select * from score where course_id = 1 or course_id = 2 ;

#根據 學號進行分組
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門課的說明是1和2都選了


#學生id已知, 匹配student名字
select student.sid,student.sname from 
(select student_id from score where course_id = 1 or course_id = 2 GROUP BY student_id having count(course_id)>1) as A
left join student on A.student_id = student.sid;

 

第8題

#8、查詢學過“李平”老師所教的所有課的同學的學號、姓名;

#先拿“李平"課程id  老師和課程先連表
select * from course left join teacher on teacher.tid = course.teacher_id;

#拿出課程id
select course.cid from course left join teacher on teacher.tid = course.teacher_id where teacher.tname = "李平老師";
#2,4

#根據2,4從score中選出學生資訊
select * from score where course_id in 
(select course.cid from course left join teacher on teacher.tid = course.teacher_id where teacher.tname = "李平老師");
#其中這些學生都選了李平的課

#選出李平的課都上的學生
select student_id from score where course_id in 
(select course.cid from course left join teacher on teacher.tid = course.teacher_id where teacher.tname = "李平老師")
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 = "李平老師");
#等於李老師的課程數量即為上了老師的所有課

 

第10題

#10、查詢有課程成績小於60分的同學的學號、姓名;

#找到有成績小於60的學生
select * from score where num < 60;

#找到有成績小於60的學生的id
select student_id from score where num < 60;

#並根據學號分組,因為有重複id出現,掛了多科的
select student_id from score where num < 60 GROUP BY student_id;
select distinct student_id from score where num < 60;#去重第二個方法

#和student進行連表 
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;

#第二個方法
select sid,sname from student where sid in (
        select distinct student_id from score where num < 60
    )

 

第11題

#11、查詢沒有學全所有課的同學的學號、姓名;

#獲得課程總數
select count(cname) from course;
#select count(cid) from course;#主鍵速度更快

#每個學生id的上的課數, 如果不寫 group by student_id會報錯因為是根據學生id分組的
select student_id, count(course_id) as course_num from score group by student_id; 

#找出未選滿的學生id
select student_id from
(select student_id, count(course_id) as course_num from score group by student_id) as A
where A.course_num not in (select count(cname) from course);

#對應名字
select sid,sname from student where sid in 
(
select student_id from
(select student_id, count(course_id) as course_num from score group by student_id) as A
where A.course_num not in (select count(cname) from course)
);

 

第12題

#12、查詢  至少有一門課與學號為“001”的同學所學相同的同學的學號和姓名;
#即和001一起上過課的

#選出了課程001號同學的課程id
select course_id from score where student_id = 1;

#選出和001號一起學習的ID
select DISTINCT student_id from score where course_id in (select course_id from score where student_id  = 1);

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;
#還需排除自身