From f2e48b8610dad05ada8d5007afa9a145eee45972 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=AB=A0=E7=AC=91=E5=AE=B9?= Date: Sun, 22 Oct 2023 23:59:12 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E6=B9=AB=E5=AE=9D=E7=9A=84=E6=97=A5?= =?UTF-8?q?=E5=BF=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\347\273\217\345\205\27050\351\242\230.md" | 418 ++++++++++++++++++ 1 file changed, 418 insertions(+) create mode 100644 "10 \347\253\240\347\254\221\345\256\271/Day1020 mysql\347\273\217\345\205\27050\351\242\230.md" diff --git "a/10 \347\253\240\347\254\221\345\256\271/Day1020 mysql\347\273\217\345\205\27050\351\242\230.md" "b/10 \347\253\240\347\254\221\345\256\271/Day1020 mysql\347\273\217\345\205\27050\351\242\230.md" new file mode 100644 index 0000000..4ecdcca --- /dev/null +++ "b/10 \347\253\240\347\254\221\345\256\271/Day1020 mysql\347\273\217\345\205\27050\351\242\230.md" @@ -0,0 +1,418 @@ +# 今日份日志 + +初步接触mysql经典50题,复习以往的知识点 + + + +# 练习 + +```mysql + +# 学生表 +CREATE TABLE IF NOT EXISTS `student`( + `student_id` INT(10) NOT NULL AUTO_INCREMENT COMMENT '学号', + `student_name` VARCHAR(10) NOT NULL DEFAULT '匿名' COMMENT '姓名', + `birthday` DATETIME NOT NULL COMMENT '出生日期', + `gender` VARCHAR(10) NOT NULL DEFAULT '男' COMMENT '性别', + PRIMARY KEY(`student_id`) +); + +INSERT INTO `student` VALUES +(1 , '赵雷' , '1990-01-01' , '男'), +(2 , '钱电' , '1990-12-21' , '男'), +(3 , '孙风' , '1990-12-20' , '男'), +(4 , '李云' , '1990-12-06' , '男'), +(5 , '周梅' , '1991-12-01' , '女'), +(6 , '吴兰' , '1992-01-01' , '女'), +(7 , '郑竹' , '1989-01-01' , '女'), +(8 , '张三' , '2017-12-20' , '女'), +(9 , '李四' , '2017-12-25' , '女'), +(10 , '李四' , '2012-06-06' , '女'), +(11 , '赵六' , '2013-06-13' , '女'), +(12 , '孙七' , '2014-06-01' , '女'); +-- 课程表 +CREATE TABLE IF NOT EXISTS `course`( + `course_id` INT(4) NOT NULL AUTO_INCREMENT COMMENT '课程编号', + `course_name` VARCHAR(10) NOT NULL COMMENT '课程名', + `teacher_id` INT(10) NOT NULL COMMENT '任课教师工号', + PRIMARY KEY(`course_id`) +); + +INSERT INTO `course` VALUES +(1, '语文', 2), +(2, '数学', 1), +(3, '英语', 3); +# 教师表 +CREATE TABLE IF NOT EXISTS `teacher`( + `teacher_id` INT(10) NOT NULL AUTO_INCREMENT COMMENT '教师工号', + `teacher_name` VARCHAR(10) NOT NULL DEFAULT '匿名' COMMENT '教师姓名', + PRIMARY KEY(`teacher_id`) +); + +INSERT INTO `teacher` VALUES +(1, '高斯'), +(2, '李白'), +(3, 'Trump'); + +-- 成绩表 +CREATE TABLE IF NOT EXISTS `score`( + `student_id` INT(10) NOT NULL COMMENT '学号', + `course_id` INT(4) NOT NULL COMMENT '课程编号', + `score` DECIMAL(18,1) COMMENT '成绩', + KEY(`course_id`) +); + +INSERT INTO `score` VALUES +(1 , 1 , 80), +(1 , 2 , 90), +(1 , 3 , 99), +(2 , 1 , 70), +(2 , 2 , 60), +(2 , 3 , 80), +(3 , 1 , 80), +(3 , 2 , 80), +(3 , 3 , 80), +(4 , 1 , 50), +(4 , 2 , 30), +(4 , 3 , 20), +(5 , 1 , 76), +(5 , 2 , 87), +(6 , 1 , 31), +(6 , 3 , 34), +(7 , 2 , 89), +(7 , 3 , 98); + + + + +# +# ``` +# +# ## 题目: +# # +# # 1. 查询" 1 "课程比" 2 "课程成绩高的学生的信息(学号、姓名、性别、出生日期)及课程分数 +select * from student join score s on student.student_id = s.student_id + join course c on s.course_id = c.course_id; +with + sc_1 as (select * from score where course_id = 1), + sc_2 as (select * from score where course_id = 2) +select s.*,sc_1.score from sc_1,sc_2,student s + where sc_1.student_id = sc_2.student_id + and s.student_id = sc_1.student_id + and sc_1.score>sc_2.score; + +# # 2. 查询同时参与" 1 "课程和" 2 "课程考试的学生信息 +# select * from score where course_id between 1 and 2; +with + co_1 as (select * from score where course_id = 1), + co_2 as (select * from score where course_id = 2) +select student.* from student,co_1,co_2 + where student.student_id = co_1.student_id + and co_1.student_id=co_2.student_id; + +# # 3. 查询存在" 1 "课程但可能不存在" 2 "课程的情况(不存在时显示为 null ) +with + co_1 as (select * from score where course_id = 1), + co_2 as (select * from score where course_id = 2) +select student.* from student right join co_1 on student.student_id = co_1.student_id + left join co_2 on co_1.student_id=co_2.student_id; + +# # 4. 查询不存在" 1 "课程但存在" 2 "课程的情况 +select * from student where student_id in ( +select student_id from score where score.student_id not in (select student_id from score where course_id = 1)); + +# # 5. 查询平均成绩大于等于 60 分的同学的学生编号、姓名和平均成绩 +select + student.student_id, + student.student_name, + avg(score) +from + score,student +where + score.student_id = student.student_id +group by + student_id +having + avg(score) >= 60; +# # 6. 查询在成绩表存在成绩的学生信息 +select * from student where student_id in ( +select distinct student_id from score where score is not null); + +# # 7. 查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩(没成绩的显示为 null ) +select + distinct s.student_id , + s.student_name, + count(course_id) over (partition by score.student_id) 选课总数, + sum(score) over (partition by score.student_id) 总成绩 +from + score join student s on score.student_id = s.student_id; + +# # 8. 查询「李」姓老师的数量 +select count(teacher_id) from teacher where teacher_name like '李% '; +# # 9. 查询学过「李白」老师授课的同学的信息 +select + s2.* +from + teacher join course c on teacher.teacher_id = c.teacher_id + join score s on c.course_id = s.course_id + join student s2 on s.student_id = s2.student_id +where teacher_name = '李白'; + +# # 10. 查询没有学全所有课程的同学的信息 +select * from student where student_id in ( +select student_id from score group by student_id having count(*) < 3); + +# # 11. 查询至少有一门课与学号为" 1 "的同学所学相同的同学的信息 +select * from student where student_id in ( +select distinct student_id from score where course_id in ( +select course_id from score where student_id = 1) and student_id != 1); + +# # 12. 查询和" 1 "号的同学学习的课程完全相同的其他同学的信息 +with + a as ( + select * from student where student_id in ( + select student_id from score where course_id = ( + select course_id from score where student_id = 1 limit 0,1))), + b as( + select * from student where student_id in ( + select student_id from score where course_id = ( + select course_id from score where student_id = 1 limit 1,1))), + c as( + select * from student where student_id in ( + select student_id from score where course_id = ( + select course_id from score where student_id = 1 limit 2,1))) +select a.* +from a join b on a.student_id = b.student_id + join c on c.student_id = a.student_id +where a.student_id != 1; + + +# # 13. 查询没学过「李白」老师讲授的任一门课程的学生姓名 +select student_name from student where student_id not in ( +select student_id from score where course_id = ( +select course_id from course where teacher_id = ( +select teacher_id from teacher where teacher_name = '李白'))); + +# # 14. 查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩 +with + a as (select student_id from score where score < 60 group by student_id having count(student_id) >= 2) +select + s.student_id, + student_name, + avg(score) 平均分 +from a join score on a.student_id = score.student_id + join student s on a.student_id = s.student_id +group by s.student_id; +# # 15. 检索" 1 "课程分数小于 60,按分数降序排列的学生信息 +select * from student where student_id in ( +select student_id from score where course_id = 1 and score < 60 order by score desc ); + +# # 16. 按平均成绩从高到低显示所有学生的所有课程的成绩以及平均成绩 +select *,avg(score) over (partition by student_id order by score desc ) 平均成绩 from score; + +# # 17. 查询各科成绩最高分、最低分和平均分 +select course_id, max(score),min(score),avg(score) from score group by course_id; + +# # 18. 以如下形式显示:课程 ID,课程 name,最高分,最低分,平均分,及格率,中等率,优良率,优秀率( +# 及格为>=60,中等为:70-80,优良为:80-90,优秀为:>=90),要求输出课程号和选修人数,查询结果按人数降序排列,若人数相同,按课程号升序排列 +select + c.course_id 课程ID , + c.course_name 课程name, + max(score) 最高分, + min(score) 最低分, + round(avg(score),2) 平均分, + concat(round(sum(case when score >= 60 then 1 else 0 end)/count(*)*100,2),'%') 及格率, + concat(round(sum(case when score between 70 and 80 then 1 else 0 end)/count(*)*100,2),'%') 中等率, + concat(round(sum(case when score between 80 and 90 then 1 else 0 end)/count(*)*100,2),'%') 优良率, + concat(round(sum(case when score >= 90 then 1 else 0 end)/count(*)*100,2),'%') 优秀率 +from score s join course c on s.course_id = c.course_id +group by c.course_id; + + +# # 19. 按各科成绩进行排序,并显示排名,Score 重复时保留名次空缺 +select *,rank() over (partition by course_id order by score desc ) 排名 from score; + +# # 20. 按各科成绩进行排序,并显示排名,Score 重复时合并名次 +select *,dense_rank() over (partition by course_id order by score desc ) 排名 from score; + +# # 21. 查询学生的总成绩,并进行排名,总分重复时保留名次空缺 +select distinct student_id,sum(score) over (partition by student_id ) 总成绩 from score order by 总成绩 desc ; + +# # 22. 查询学生的总成绩,并进行排名,总分重复时不保留名次空缺 +select distinct student_id,sum(score) over (partition by student_id ) 总成绩 from score order by 总成绩 desc ; + + +# # 23. 统计各科成绩各分数段人数:课程编号,课程名称,[100-85],[85-70],[70-60],[60-0] 及所占百分比 +select + course_id, + (select course_name from course where score.course_id = course_id) 课程名称, + sum(case when score <=100 and score >= 85 then 1 else 0 end ) `[100-85]`, + sum(case when score <85 and score >= 70 then 1 else 0 end ) `[85-70]`, + sum(case when score <70 and score >= 60 then 1 else 0 end ) `[70-60]`, + sum(case when score <60 and score >=0 then 1 else 0 end ) `[60-0]`, + + sum(case when score <=100 and score >= 85 then 1 else 0 end )/count(*), + sum(case when score <85 and score >= 70 then 1 else 0 end )/count(*), + sum(case when score <70 and score >= 60 then 1 else 0 end )/count(*), + sum(case when score <60 and score >=0 then 1 else 0 end ) /count(*) +from + score +group by course_id; + + + + + + + + + +# # 24. 查询各科成绩前三名的记录 +select + c.course_id,course_name,s.student_id,student_name,score +from + (select * + from score s1 + where (select count(*) + from score s2 + where s1.course_id = s2.course_id and s1.score < s2.score) < 3) t1 +join student s on s.student_id = t1.student_id +join course c on c.course_id = t1.course_id +order by c.course_id,score desc ; +# # 25. 查询每门课程被选修的学生数 +select + * +from + course c join + (select course_id,count(*) + from score group by course_id) t1 on c.course_id = t1.course_id; +# # 26. 查询出只选修两门课程的学生学号和姓名 +select + student_id,student_name +from student +where student_id in ( +select + student_id +from + score +group by student_id having count(*) = 2); +# # 27. 查询男生、女生人数 +select gender,count(*) from student group by gender; +# # 28. 查询名字中含有「风」字的学生信息 +select * from student where student_name like '%风%'; +# # 29. 查询同名同性学生名单,并统计同名人数 +select + count(*) +from student + group by student_name,gender + having count(*) > 1; + +# # 30. 查询 1990 年出生的学生名单 +select * from student where year(birthday) = 1990; + +# # 31. 查询每门课程的平均成绩,结果按平均成绩降序排列,平均成绩相同时,按课程编号升序排列 +select course_id, avg(score) from score group by course_id order by avg(score) desc ,course_id; +# # 32. 查询平均成绩大于等于 85 的所有学生的学号、姓名和平均成绩 +with + a as( +select student_id,avg(score) avg_score from score group by student_id having avg(score) > 85) +select s.student_id,student_name,avg_score from a join student s on a.student_id = s.student_id; +# # 33. 查询课程名称为「数学」,且分数低于 60 的学生姓名和分数 +with + a as ( +select * from score where course_id = ( +select course_id from course where course_name = '数学') and score < 60) +select * from student s join a on s.student_id = a.student_id; +# # 34. 查询所有学生的课程及分数情况(存在学生没成绩,没选课的情况) +select + s.student_id,student_name, + sum(case course_id when 1 then score else 0 end) 语文, + sum(case course_id when 2 then score else 0 end) 数学, + sum(case course_id when 3 then score else 0 end) 英语 +from student st join score s on st.student_id = s.student_id +group by s.student_id; +# # 35. 查询任何一门课程成绩在 70 分以上的姓名、课程名称和分数 +select + student_name,course_name,score +from + score sc join student s on sc.student_id = s.student_id + join course c on sc.course_id = c.course_id +where score > 70; +# # 36. 查询不及格的课程 +select + student_id,course_name,score +from + score s join course c on s.course_id = c.course_id +where score < 60; +# # 37. 查询课程编号为 1 且课程成绩在 80 分以上的学生的学号和姓名 +select + student_id,student_name +from + student st +where student_id in ( + select student_id from score where course_id = 1 and score > 80 + ); + +# # 38. 求每门课程的学生人数 +select + c.course_id,course_name,cou_stu +from + course c join (select course_id,count(*) cou_stu from score group by course_id) t1 +on c.course_id = t1.course_id; + +# # 39. 成绩不重复,查询选修「张三」老师所授课程的学生中,成绩最高的学生信息及其成绩 +select + s.*,score +from + student s join (select student_id,score from score sc join (select course_id from course c join teacher t on c.teacher_id = t.teacher_id where teacher_name = '张三') + t2 on sc.course_id = t2.course_id order by score desc limit 1) t1 + on s.student_id = t1.student_id; + + +# # 40. 成绩有重复的情况下,查询选修「张三」老师所授课程的学生中,成绩最高的学生信息及其成绩 +select + s.*,score +from + student s join (select student_id,score from score sc join (select course_id from course c join teacher t on c.teacher_id = t.teacher_id where teacher_name = '张三') + t2 on sc.course_id = t2.course_id order by score desc limit 1) t1 + on s.student_id = t1.student_id; + +# # 41. 查询不同课程成绩相同的学生的学生编号、课程编号、学生成绩 + +with + a as ( select * from score where course_id = 1 ), + b as ( select * from score where course_id = 2 ), + c as ( select * from score where course_id = 3 ) +select * from a join b on a.student_id = b.student_id + join c on a.student_id = c.student_id +where a.score = b.score or a.score = c.score or b.score = c.score; +# # 42. 查询每门功成绩最好的前两名、 +with + a as (select *,row_number() over (partition by course_id order by score desc) 排名 from score) +select * from a where 排名 <= 2; + +# # 43. 统计每门课程的学生选修人数(超过 5 人的课程才统计)。 +select course_id, count(*) from score group by course_id having count(*)>5; +# # 44. 检索至少选修两门课程的学生学号 +select student_id from score group by student_id having count(*) >=2; +# # 45. 查询选修了全部课程的学生信息 +select * from student where student_id in ( +select student_id from score group by student_id having count(*) = 3); +# # 46. 查询各学生的年龄,只按年份来算 +select student_name ,year(now())-year(birthday) from student; +# # 47. 按照出生日期来算,当前月日 < 出生年月的月日则,年龄减一 +# select * from student; +select student_name,timestampdiff(year,birthday,now()) from student; +# # 48. 查询本周过生日的学生 +select * from student +where week(birthday,1) = week(now(),1); +# # 49. 查询下周过生日的学生 +select * from student +where week(birthday,1) = week(now(),1)+1; +# # 50. 查询本月过生日的学生 +select * from student where month(birthday) = month(now()); +# # 51. 查询下月过生日的学生 +select * from student where month(birthday) = month(now())+1; + +``` + -- Gitee From 527cf75fe35bf0b98539d5bdc46c9734e9547cc4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=AB=A0=E7=AC=91=E5=AE=B9?= Date: Mon, 23 Oct 2023 23:54:31 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E6=B9=AB=E5=AE=9D=E7=9A=84=E6=97=A5?= =?UTF-8?q?=E5=BF=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...y1023 mysql\345\210\235\345\244\215\344\271\240.md" | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 "10 \347\253\240\347\254\221\345\256\271/Day1023 mysql\345\210\235\345\244\215\344\271\240.md" diff --git "a/10 \347\253\240\347\254\221\345\256\271/Day1023 mysql\345\210\235\345\244\215\344\271\240.md" "b/10 \347\253\240\347\254\221\345\256\271/Day1023 mysql\345\210\235\345\244\215\344\271\240.md" new file mode 100644 index 0000000..a8e3092 --- /dev/null +++ "b/10 \347\253\240\347\254\221\345\256\271/Day1023 mysql\345\210\235\345\244\215\344\271\240.md" @@ -0,0 +1,10 @@ +# 今日份日志 + +今日份复习: + +1. er图 +2. CDM模型 +3. 范式(略过) +4. 视图 +5. 常用函数 +6. 存储过程 \ No newline at end of file -- Gitee