当前位置:Gxlcms > 数据库问题 > 重修课程day45(mysql之练习题二)

重修课程day45(mysql之练习题二)

时间:2021-07-01 10:21:17 帮助过:4人阅读

table class(cid int primary key auto_increment, caption char(5) not null unique); INSERT into class(caption)values(三年二班),(一年三班),(三年一班); CREATE table student(sid int primary key auto_increment, sname char(6) not null, gender enum(,,male,female) not null, class_id int(4) not null, foreign key(class_id) references class(cid) on delete CASCADE on update cascade); insert into student(sname,gender,class_id)values (钢蛋,,1),(铁锤,,1),(山炮,,2); create table teacher(tid int primary key auto_increment, tname char(6) not null); insert into teacher(tname)values(波多),(苍空),(饭岛); create table course(cid int primary key auto_increment, cname CHAR(5) not null unique, teacher_id int not null, foreign key(teacher_id) references teacher(tid) on delete CASCADE on update cascade); insert into course(cname,teacher_id)values(生物,1),(体育,1),(物理,2); create table score(sid int primary key auto_increment, student_id int not null, foreign key(student_id) references student(sid) on delete cascade on update cascade, course_id int not null, foreign key(course_id) references course(cid) on delete cascade on update cascade, number int(4) not null); insert into score(student_id,course_id,number)values(1,1,60),(1,2,59),(2,2,100); SELECT * from class; show CREATE table class; select * from student; show create table student; SELECT * from teacher; show create table teacher; select * from course; show create table course; select * from score; show create table score; View Code

开始练习:

  1. 1、查询所有的课程的名称以及对应的任课老师姓名
  2. SELECT cname,tname from course inner join teacher ON course.teacher_id = teacher.tid;
  3. 2、查询学生表中男女生各有多少人
  4. select gender,COUNT(sid) from student GROUP BY gender;
  5. 3、查询物理成绩等于100的学生的姓名
  6. SELECT sname from student where sid in (
  7. SELECT student_id from score where course_id = (SELECT cid from course where cname = ‘物理‘) and num = 100
  8. );
  9. 4、查询平均成绩大于八十分的同学的姓名和平均成绩
  10. 方法1:
  11. SELECT student.sname,t1.avg_num from student inner join
  12. (SELECT student_id,AVG(num) avg_num from score GROUP BY student_id
  13. HAVING avg(num) > 80) as t1
  14. on student.sid = t1.student_id;
  15. 方法2:
  16. select * from student where sid in (
  17. select student_id from score group by student_id
  18. having avg(num)>80
  19. );
  20. 5、查询所有学生的学号,姓名,选课数,总成绩
  21. SELECT student.sid,student.sname,t1.course_num,t1.total_num from student inner JOIN
  22. (SELECT
  23. student_id,
  24. count(course_id) course_num,
  25. sum(num) total_num
  26. FROM
  27. score
  28. GROUP BY
  29. student_id) as t1
  30. on student.sid = t1.student_id;
  31. 6、 查询姓李老师的个数
  32. 方法1:
  33. SELECT COUNT(1) from teacher where tname like ‘李%‘;
  34. 方法2:
  35. select count(t1) from (
  36. select tname t1 from teacher where tname LIKE ‘李%‘
  37. )as t
  38. 7、 查询没有报李平老师课的学生姓名
  39. SELECT
  40. sname
  41. FROM
  42. student
  43. WHERE
  44. sid NOT IN (
  45. SELECT
  46. student_id
  47. FROM
  48. score
  49. WHERE
  50. course_id IN (
  51. SELECT
  52. cid
  53. FROM
  54. course
  55. WHERE
  56. teacher_id = (
  57. SELECT
  58. tid
  59. FROM
  60. teacher
  61. WHERE
  62. tname = ‘李平老师‘
  63. )
  64. )
  65. );
  66. 8、 查询物理课程比生物课程高的学生的学号
  67. SELECT t1.student_id from
  68. (SELECT student_id,num from score where course_id = (
  69. SELECT cid from course where cname = ‘物理‘
  70. )) as t1
  71. inner join
  72. (SELECT student_id,num from score where course_id = (
  73. SELECT cid from course where cname = ‘生物‘
  74. )) as t2
  75. on t1.student_id = t2.student_id
  76. where t1.num > t2.num;
  77. 9、 查询没有同时选修物理课程和体育课程的学生姓名
  78. 方法1:
  79. SELECT sname from student where sid in (
  80. SELECT student_id from score LEFT JOIN course
  81. on score.course_id = course.cid
  82. WHERE course.cname in (‘物理‘,‘体育‘)
  83. GROUP BY student_id
  84. HAVING count(sid) < 2
  85. );
  86. 方法2:
  87. select sname from student where sid not in (
  88. SELECT s1.student_id from (
  89. select student_id from score where course_id =(
  90. SELECT cid from course where cname =‘体育‘)) s1
  91. INNER JOIN (
  92. select student_id from score where course_id =(
  93. SELECT cid from course where cname =‘物理‘)) s2
  94. on s1.student_id=s2.student_id);
  95. 10、查询挂科超过两门(包括两门)的学生姓名和班级
  96. 方法1::
  97. SELECT sname,caption from student LEFT JOIN class
  98. on student.class_id = class.cid
  99. where student.sid in (
  100. SELECT student_id from score where num < 60 GROUP BY student_id
  101. HAVING COUNT(course_id) >= 2
  102. )
  103. ;
  104. 方法2:
  105. select s.sname,class.caption from class INNER JOIN
  106. (select * from student where sid in (
  107. select student_id from score GROUP BY student_id
  108. having student_id>=2)) s
  109. on s.class_id=class.cid;
  110. 11 、查询选修了所有课程的学生姓名
  111. select sname from student where sid in (
  112. select student_id from score GROUP BY student_id
  113. having count(sid)=(
  114. select count(cid) from course))
  115. 12、查询李平老师教的课程的所有成绩记录
  116. 方法1:
  117. SELECT * from score where course_id in (
  118. SELECT cid from course inner JOIN teacher
  119. on course.teacher_id = teacher.tid
  120. WHERE tname = ‘李平老师‘
  121. );
  122. 方法2:
  123. select num from score WHERE course_id in (
  124. select cid from course where teacher_id=(
  125. select tid from teacher where tname=‘李平老师‘));
  126. 13、查询全部学生都选修了的课程号和课程名
  127. SELECT ss.s1,ss.s2,course.cid,course.cname from
  128. (select student.sid s1,student.sname s2,score.course_id s3
  129. from student INNER JOIN score
  130. on student.sid=score.student_id ) ss
  131. INNER JOIN course
  132. on ss.s3=course.cid;
  133. 14、查询每门课程被选修的次数
  134. 方法1:
  135. SELECT course.cname,t1.count_student FROM course
  136. INNER JOIN
  137. (
  138. SELECT course_id,count(student_id) count_student from score GROUP BY course_id
  139. ) as t1
  140. ON course.cid = t1.course_id;
  141. 方法2:
  142. select course.cname,COUNT(score.sid)
  143. from course INNER JOIN score
  144. on course.cid=score.course_id
  145. group by score.course_id;
  146. 15、查询只选修了一门课程的学生姓名和学号
  147. select sid,sname from student where sid in(
  148. select student_id from score GROUP BY student_id
  149. having count(sid)=1);
  150. 16、查询所有学生考出的总成绩并按从高到低排序(成绩去重)
  151. 方法1:
  152. SELECT DISTINCT sum(num) sum_num from score group by student_id
  153. ORDER BY sum_num desc;
  154. 方法2:
  155. select student.sname,avg(score.num) avg_num from
  156. student INNER JOIN score on student.sid=score.student_id
  157. GROUP BY student_id ORDER BY avg_num desc;
  158. 17、查询平均成绩大于85的学生姓名和平均成绩
  159. 方法1:
  160. SELECT student.sname,t1.avg_num from student inner join
  161. (
  162. SELECT student_id,avg(num) avg_num from score GROUP BY student_id having avg(num) > 85
  163. ) as t1
  164. on student.sid = t1.student_id;
  165. 方法2:
  166. select student.sname,avg(score.num) from student INNER JOIN score
  167. on student.sid=score.student_id
  168. GROUP BY score.student_id
  169. having avg(score.num)>85;
  170. 18、查询生物成绩不及格的学生姓名和对应生物分数
  171. 方法1:
  172. SELECT sname,t1.num from student
  173. INNER JOIN
  174. (
  175. SELECT student_id,num from score LEFT JOIN course
  176. on score.course_id = course.cid
  177. where course.cname = ‘生物‘ and score.num < 60
  178. ) as t1
  179. on student.sid = t1.student_id;
  180. 方法2:
  181. select student.sname,ss.num from student INNER JOIN(
  182. select * from score where course_id=(
  183. select cid from course where cname=‘生物‘) and num<60) ss
  184. on ss.student_id=student.class_id;
  185. 19、查询在所有选修了李平老师课程的学生中,这些课程(李平老师的课程,不是所有课程)平均成绩最高的学生姓名
  186. select sname from student where sid in(
  187. select student_id from score where course_id in(
  188. select cid from course where teacher_id=(
  189. select tid from teacher where tname=‘李平老师‘))
  190. GROUP BY student_id
  191. HAVING avg(num)=(
  192. select avg(num) from score where course_id in(
  193. select cid from course where teacher_id=(
  194. select tid from teacher where tname=‘李平老师‘))
  195. GROUP BY student_id order by avg(num) desc
  196. limit 1))
  197. 20、查询每门课程成绩最好的前两名学生姓名
  198. SELECT * from score ORDER BY course_id,num desc;
  199. #取得课程编号与第一高的成绩:course_id,first_num
  200. SELECT course_id,max(num) first_num from score GROUP BY course_id;
  201. #取得课程编号与第二高的成绩:course_id,second_num
  202. SELECT score.course_id,max(num) second_num from score LEFT JOIN (
  203. SELECT course_id,max(num) first_num from score GROUP BY course_id
  204. ) as t1
  205. on score.course_id = t1.course_id
  206. where score.num < t1.first_num
  207. GROUP BY score.course_id
  208. ;
  209. #链表得到一张新表,新表包含课程编号与这门课程前两名的成绩分数
  210. select t1.course_id,t1.first_num,t2.second_num from
  211. (SELECT course_id,max(num) first_num from score GROUP BY course_id) as t1
  212. inner join
  213. (SELECT score.course_id,max(num) second_num from score LEFT JOIN (
  214. SELECT course_id,max(num) first_num from score GROUP BY course_id
  215. ) as t1
  216. on score.course_id = t1.course_id
  217. where score.num < t1.first_num
  218. GROUP BY score.course_id) as t2
  219. on t1.course_id = t2.course_id;
  220. #取前两名学生的编号
  221. SELECT score.course_id,score.student_id from score LEFT JOIN (
  222. select t1.course_id,t1.first_num,t2.second_num from
  223. (SELECT course_id,max(num) first_num from score GROUP BY course_id) as t1
  224. inner join
  225. (SELECT score.course_id,max(num) second_num from score LEFT JOIN (
  226. SELECT course_id,max(num) first_num from score GROUP BY course_id
  227. ) as t1
  228. on score.course_id = t1.course_id
  229. where score.num < t1.first_num
  230. GROUP BY score.course_id) as t2
  231. on t1.course_id = t2.course_id
  232. ) as t3
  233. on score.course_id = t3.course_id
  234. where score.num >= t3.second_num and score.num <= t3.first_num
  235. ;
  236. SELECT t4.course_id,student.sname from student inner join
  237. (
  238. SELECT score.course_id,score.student_id from score LEFT JOIN (
  239. select t1.course_id,t1.first_num,t2.second_num from
  240. (SELECT course_id,max(num) first_num from score GROUP BY course_id) as t1
  241. inner join
  242. (SELECT score.course_id,max(num) second_num from score LEFT JOIN (
  243. SELECT course_id,max(num) first_num from score GROUP BY course_id
  244. ) as t1
  245. on score.course_id = t1.course_id
  246. where score.num < t1.first_num
  247. GROUP BY score.course_id) as t2
  248. on t1.course_id = t2.course_id
  249. ) as t3
  250. on score.course_id = t3.course_id
  251. where score.num >= t3.second_num and score.num <= t3.first_num
  252. ) as t4
  253. on student.sid = t4.student_id
  254. ORDER BY t4.course_id
  255. ;
  256. select student.sname,t.course_id,t.num from student INNER JOIN
  257. (
  258. select
  259. s1.student_id,s1.course_id,s1.num,
  260. (select num from score as s2 where s2.course_id = s1.course_id order by num desc limit 0,1) as first_num,
  261. (select num from score as s2 where s2.course_id = s1.course_id order by num desc limit 1,1) as second_num
  262. from
  263. score as s1
  264. ) as t
  265. on student.sid = t.student_id
  266. where t.num in (t.first_num,t.second_num)
  267. ORDER BY t.course_id
  268. ;
  269. SELECT sid from score as s1 ;

 

重修课程day45(mysql之练习题二)

标签:date   close   多少   span   cad   技术   play   log   esc   

人气教程排行