1. 程式人生 > >第四天(41道題全解)

第四天(41道題全解)

rom blog str case 循環 clas 同名 http name

41道題:

技術分享圖片
重要的
連表查詢
分組

制表語句:
    班級表
               Table: class
        Create Table: CREATE TABLE `class` (
          `cid` int(11) NOT NULL AUTO_INCREMENT,
          `caption` varchar(50) DEFAULT NULL,
          PRIMARY KEY (`cid`)
        ) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8
        
1 row in set (0.00 sec) 學生表: Table: student Create Table: CREATE TABLE `student` ( `sid` int(11) NOT NULL AUTO_INCREMENT, `sname` varchar(50) DEFAULT NULL, `gender` char(10) DEFAULT NULL, `class_id` int(11) DEFAULT NULL, PRIMARY KEY (`sid`), KEY `fk_student_class` (`class_id`), CONSTRAINT `fk_student_class` FOREIGN KEY (`class_id`) REFERENCES `
class` (`cid`) ) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8 老師表: Table: teacher Create Table: CREATE TABLE `teacher` ( `tid` int(11) NOT NULL AUTO_INCREMENT, `tname` varchar(50) DEFAULT NULL, PRIMARY KEY (`tid`) ) ENGINE
=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8 1 row in set (0.00 sec) 課程表: Table: course Create Table: CREATE TABLE `course` ( `cid` int(11) NOT NULL AUTO_INCREMENT, `cname` char(25) DEFAULT NULL, `teacher_id` int(11) DEFAULT 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=4 DEFAULT CHARSET=utf8 1 row in set (0.00 sec) 成績表: create table score (sid int not null auto_increment primary key, student_id int not null, corse_id int not null, number int not null, unique uq_sc (student_id,corse_id), CONSTRAINT fk_sc_st FOREIGN key (student_id) REFERENCES student(sid), constraint fk_sc_co foreign key (corse_id) references course(cid) ) engine=innodb default charset=utf8; group by前面的slect 只能用聚合函數count,max什麽的 v= 111 if 1==1 else 110 三元運算符 ######################################################### 作業練習: http://www.cnblogs.com/wupeiqi/articles/5729934.html http://www.cnblogs.com/wupeiqi/p/5748496.html 參考答案 ########################################################## 用到的知識點 - 臨時表 select * from (select * from tb where id< 10) as B; --- select id, name, 1, (select count(1) from tb)----只要拿到的是一個值就可以 from tb2 ---把最外層的表s1,每次循環取一個常量值再到每一列中再循環一次 SELECT student_id, (select num from score as s2 where s2.student_id=s1.student_id and course_id = 1) as 語文, (select num from score as s2 where s2.student_id=s1.student_id and course_id = 2) as 數學, (select num from score as s2 where s2.student_id=s1.student_id and course_id = 3) as 英語 from score as s1; ----avg(if(isnull(score.num),0,score.num)) 三目運算符 ##################################################### 具體作業: 1、自行創建測試數據 2、查詢“生物”課程比“物理”課程成績高的所有學生的學號; PS:查出生物課程的學生,查出物理課程的學生,用臨時表,再連接查詢 select A.student_id from (select * from score LEFT JOIN course on score.course_id=course.cid where course.cname="生物" ) as A LEFT JOIN (select * 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 3、查詢平均成績大於60分的同學的學號和平均成績; select student_id, avg(num) from score GROUP BY student_id PS:把名字也帶上 select S.student_id,student.sname,S.av_s from (SELECT student_id, avg(num) AS av_s FROM score GROUP BY student_id HAVING av_s > 60) as S LEFT JOIN student on student.sid=S.student_id 4、查詢所有同學的學號、姓名、選課數、總成績; SELECT score.student_id,student.sname, COUNT(score.student_id),sum(score.num) from score LEFT JOIN student ON score.student_id=student.sid GROUP BY score.student_id 5、查詢姓“李”的老師的個數; select COUNT(*) from teacher where teacher.tname LIKE "李%" 6、查詢沒學過“李平老師“課的同學的學號、姓名; PS:先查找李平的課程ID,再到score表中找學他的課的學生Id,再去student表中全部排除 select student.sid,student.sname from student where student.sid not in ( select score.student_id from score where course_id in (select course.cid from course LEFT JOIN teacher on teacher.tid=course.cid where teacher.tname="李平老師") ) 7、查詢學過“001”並且也學過編號“002”課程的同學的學號、姓名; PS:找出score表中學過課程1,或者2,或者1,2的學生,分組找出大於2的,再與學生表進行連接查詢 select student.sid,student.sname from score LEFT JOIN student on score.student_id=student.sid where score.course_id=1 or score.course_id=2 GROUP BY score.student_id HAVING count(score.course_id)>1 8、查詢學過“葉平”老師所教的所有課的同學的學號、姓名; PS:語句分兩部分B表是查出學過葉平老師所有課程的學生ID(註意其中葉平的課程有所變化所以不能count >2) 再與學生表進行關聯 SELECT student.sid,student.sname FROM student LEFT JOIN ( SELECT score.student_id FROM score WHERE score.course_id IN ( SELECT cid FROM course LEFT JOIN teacher ON teacher.tid = course.teacher_id WHERE teacher.tname = "李平老師" ) --查出葉平老師所教的課程的ID GROUP BY score.student_id HAVING count(score.student_id) = ( SELECT count(cid) FROM course LEFT JOIN teacher ON teacher.tid = course.teacher_id WHERE teacher.tname = "李平老師" ) --查出葉平老師總共教了幾門課count(cid) ) AS B --查出學過葉平老師所有課程的學生ID ON student.sid = B.student_id 9、查詢課程編號“002”的成績比課程編號“001”課程低的所有同學的學號、姓名; PS: select student.sid,student.sname from student left JOIN (select A.student_id from (select * from score LEFT JOIN course on score.course_id=course.cid where course.cid=1 ) as A LEFT JOIN (select * from score LEFT JOIN course on score.course_id=course.cid where course.cid=2 ) as B on A.student_id=B.student_id where A.num<B.num ) as C on student.sid=C.student_id 10、查詢有課程成績小於60分的同學的學號、姓名; select sid,sname from student where sid in ( select distinct student_id from score where num < 60 ) 11、查詢沒有學全所有課的同學的學號、姓名; PS ---count要嘛用1要嘛用主鍵,效率高 SELECT student.sid,student.sname from student INNER JOIN ( select score.student_id,count(1) from score GROUP BY score.student_id HAVING count(1) < (SELECT count(cid) from course) ) as B on student.sid = B.student_id 12、查詢至少有一門課與學號為“001”的同學所學相同的同學的學號和姓名; select student.sid,student.sname from student INNER JOIN ( SELECT score.student_id,score.course_id from score where score.student_id !=1 and score.course_id in (select score.course_id from score where score.student_id=1) GROUP BY score.student_id ) as B on student.sid=B.student_id 13、查詢至少學過學號為“001”同學所有課的其他同學學號和姓名 select student.sid,student.sname from student INNER JOIN ( SELECT score.student_id,count(1) from score where score.student_id !=1 and score.course_id in (select score.course_id from score where score.student_id=1) GROUP BY score.student_id HAVING count(1) = (select count(1) from score where score.student_id=1) ) AS B on student.sid=B.student_id 14、查詢和“002”號的同學學習的課程完全相同的其他同學學號和姓名; SELECT student_id from score where student_id in ( select student_id from score where student_id !=2 GROUP BY student_id HAVING COUNT(1) =(select count(1) from score where student_id=1) ) and course_id in (SELECT course_id from score where student_id =2) GROUP BY student_id HAVING COUNT(1) = (SELECT count(1) from score where student_id) 15、刪除學習“葉平”老師課的SC表記錄; delete from score where course_id in ( select cid from course left join teacher on course.teacher_id = teacher.tid where teacher.name = 葉平 ) 16、向SC表中插入一些記錄,這些記錄要求符合以下條件:①沒有上過編號“002”課程的同學學號;②插入“002”號課程的平均成績; insert into score(student_id,course_id,num) VALUES(select student_id,2,(select AVG(num) from score where course_id = 2) from score where course_id !=2) 17、按平均成績從低到高顯示所有學生的“語文”、“數學”、“英語”三門的課程成績,按如下形式顯示: 學生ID,語文,數學,英語,有效課程數,有效平均分; select s1.student_id, (select num from score left join course on score.course_id = course.cid where course.cname = "生物" and score.student_id=s1.student_id) as 生物, (select num from score left join course on score.course_id = course.cid where course.cname = "物理" and score.student_id=s1.student_id) as 物理, (select num from score left join course on score.course_id = course.cid where course.cname = "體育" and score.student_id=s1.student_id) as 體育, count(s1.course_id), avg(s1.num) from score as s1 group by student_id desc 18、查詢各科成績最高和最低的分:以如下形式顯示:課程ID,最高分,最低分; SELECT course_id,MAX(num),min(num) from score GROUP BY course_id 19、按各科平均成績從低到高和及格率的百分數從高到低順序; select course_id,avg(num), sum(case when num <60 THEN 0 ELSE 1 END),sum(1), sum(case when num <60 THEN 0 ELSE 1 END)/sum(1) as jgl from score GROUP BY course_id order by AVG(num) asc,jgl desc; 20、課程平均分從高到低顯示(現實任課老師); select course_id,avg(num),teacher.tname from score LEFT JOIN course on score.course_id=course.cid left JOIN teacher on teacher.tid = course.teacher_id GROUP BY course_id desc select avg(if(isnull(score.num),0,score.num)),teacher.tname from course left join score on course.cid = score.course_id left join teacher on course.teacher_id = teacher.tid group by score.course_id 21、查詢各科成績前三名的記錄:(不考慮成績並列情況) 22、查詢每門課程被選修的學生數; SELECT student_id ,count(1) from score GROUP BY course_id HAVING COUNT(1) >5 23、查詢出只選修了一門課程的全部學生的學號和姓名; SELECT student.sid,student.sname from student INNER JOIN (SELECT student_id ,count(1) from score GROUP BY student_id HAVING COUNT(1) =1) as B on student.sid=B.student_id 24、查詢男生、女生的人數; SELECT gender,COUNT(1) from student GROUP BY gender 25、查詢姓“張”的學生名單; SELECT sname from student where sname LIKE "張%" 26、查詢同名同姓學生名單,並統計同名人數; SELECT sname,count(1) from student GROUP BY sname 27、查詢每門課程的平均成績,結果按平均成績升序排列,平均成績相同時,按課程號降序排列; SELECT course_id,AVG(num) from score GROUP BY score.course_id ORDER BY avg(num) ASC,course_id DESC 28、查詢平均成績大於85的所有學生的學號、姓名和平均成績; SELECT sid,sname,T.B from student INNER JOIN (SELECT student_id ,avg(if(ISNULL(num) ,0,num)) as B from score GROUP BY student_id HAVING B >85) as T on T.student_id=student.sid 29、查詢課程名稱為“生物”,且分數低於60的學生姓名和分數; SELECT student.sname,score.num from course LEFT JOIN score on course.cid=score.course_id LEFT JOIN student on student.sid=score.student_id where course.cname=生物 and score.num < 60 30、查詢課程編號為003且課程成績在80分以上的學生的學號和姓名; SELECT student.sname,score.num from course LEFT JOIN score on course.cid=score.course_id LEFT JOIN student on student.sid=score.student_id where course.cid=3 and score.num >80 31、求選了課程的學生人數 SELECT count(DISTINCT(score.student_id)) from score 32、查詢選修“李平”老師所授課程的學生中,成績最高的學生姓名及其成績; SELECT student.sname,T.num from student INNER JOIN (SELECT * from teacher LEFT JOIN course on teacher.tid=course.teacher_id LEFT JOIN score on score.course_id=course.cid GROUP BY num HAVING teacher.tname="張磊老師" ORDER BY num DESC LIMIT 0,1) as T on student.sid= T.student_id 33、查詢各個課程及相應的選修人數; select course.cname,count(1) from score left join course on score.course_id = course.cid group by course_id; 34、查詢不同課程但成績相同的學生的學號、課程號、學生成績; SELECT DISTINCT s1.student_id,s1.course_id,s1.num from score as s1,score as s2 WHERE s1.sid !=s2.sid and s1.course_id !=s2.course_id and s1.num=s2.num 35、查詢每門課程成績最好的前兩名; select score.sid,score.course_id,score.num,T.first_num,T.second_num from score left join ( select sid, (select num from score as s2 where s2.course_id = s1.course_id order by num desc limit 0,1) as first_num, (select num from score as s2 where s2.course_id = s1.course_id order by num desc limit 1,1) as second_num from score as s1 ) as T on score.sid =T.sid where score.num <= T.first_num and score.num >= T.second_num 36、檢索至少選修兩門課程的學生學號; SELECT student_id from score GROUP BY student_id HAVING count(student_id) >1 37、查詢全部學生都選修的課程的課程號和課程名; 38、查詢沒學過“葉平”老師講授的任一門課程的學生姓名; PS:先查出學過李平老師任意一門課的學生,包括只選了一門的,然後,全部排除 SELECT student.sid from student WHERE sid not in( SELECT score.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="李平老師" )) 39、查詢兩門以上不及格課程的同學的學號及其平均成績; select student_id,count(1) from score where num < 60 group by student_id having count(1) > 2 40、檢索“004”課程分數小於60,按分數降序排列的同學學號; select student_id from score where num< 60 and course_id = 4 order by num desc; 41、刪除“002”同學的“001”課程的成績; delete from score where course_id = 1 and student_id = 2
View Code

第四天(41道題全解)