From 12040edb296861783282886e0c2be6cccb9c49b2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=B7=AF=E7=8E=B2?= <1516489926@qq.com> Date: Mon, 23 Oct 2023 01:06:33 +0800 Subject: [PATCH] =?UTF-8?q?50=E9=A2=98=E5=A4=8D=E4=B9=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...50\351\242\230\345\244\215\344\271\240.md" | 388 ++++++++++++++++++ 1 file changed, 388 insertions(+) create mode 100644 "29 \350\267\257\347\216\262/20231021 50\351\242\230\345\244\215\344\271\240.md" diff --git "a/29 \350\267\257\347\216\262/20231021 50\351\242\230\345\244\215\344\271\240.md" "b/29 \350\267\257\347\216\262/20231021 50\351\242\230\345\244\215\344\271\240.md" new file mode 100644 index 0000000..7453158 --- /dev/null +++ "b/29 \350\267\257\347\216\262/20231021 50\351\242\230\345\244\215\344\271\240.md" @@ -0,0 +1,388 @@ +## 笔记 + +```mysql +group_concat() +是将分组中括号里对应的字符串进行连接.如果分组中括号里的参数xxx有多行,那么就会将这多行的字符串连接,每个字符串之间会有特定的符号进行分隔。 +语法示意 +# 将分组中column1这一列对应的多行的值按照column2 升序或者降序进行连接,其中分隔符为seq +# 如果用到了DISTINCT,将表示将不重复的column1按照column2升序或者降序连接 +# 如果没有指定SEPARATOR的话,也就是说没有写,那么就会默认以 ','分隔 +GROUP_CONCAT([DISTINCT] column1 [ORDER BY column2 ASC\DESC] [SEPARATOR seq]); +语法解释 +[ ORDER BY column2 ASC\DESC] :表示将会根据column2升序或者降序连接.其中column2不一定一定要求是column1,只要保证column2在这个分组中即可.如果没有写ORDER BY句段,那么连接是没有顺序的。 +[ SEPARATOR seq] : 表示各个column1将会以什么分隔符进行分隔,例如SEPARATOR '' ,则表示column1将会以进行分隔。如果没有指定seq的时候,也即没有写SEPARATOR seq这个句段,那么就会默认是以,分隔的。 +CONCAT函数中要连接的数据含有NULL,最后返回的是NULL,但是GROUP_CONCAT不会这样,他会忽略NULL值。 + + +mysql中week()函数是用来做周的统计和计算,返回日期的周数 + +例如统计今年每周有多少个注册用户 + +SELECT count(id) as count,week(create_time,1) as weeks FROM user WHERE create_time > ‘2020’ and create_time<‘2011’ GROUP BY weeks; + +sql里面的weeks就是第几周 + +WEEK(date, mode);有两个参数 + +1、date是要获取周数的日期 + +2、mode是一个可选参数,用于确定周数计算的逻辑 + +mode参数比较难理解,下面做个表格解释一下 + +模式 一周的第一天 范围 说明 +0 星期日 0-53 +遇到本年的第一个星期天开始,是第一周。前面的计算为第0周。 + +1 星期一 0-53 假如第一周能超过3天,那么计算为本年的第一周。否则为第0周 +2 星期日 1-53 遇到本年的第一个星期天开始,是第一周。 +3 星期一 1-53 假如第一周能超过3天,那么计算为本年的第一周。否则为上年度的第5x周。 +4 星期日 0-53 假如第一周能超过3天,那么计算为本年的第一周。否则为第0周 +5 星期一 0-53 遇到本年的第一个星期一开始,是第一周。 +6 星期日 1-53 假如第一周能超过3天,那么计算为本年的第一周。否则为上年度的第5x周。 +7 星期一 1-53 遇到本年的第一个星期一开始,是第一周。 +———————————————— +版权声明:本文为CSDN博主「芒果yk」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。 +原文链接:https://blog.csdn.net/liumeixiang777/article/details/119243265 +``` + +## 作业 + +```mysql +create database work2_23 charset utf8; +use work2_23; +# 学生表 +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`) +)ENGINE=INNODB CHARSET=utf8; + +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`) +)ENGINE=INNODB CHARSET=utf8; + +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`) +)ENGINE=INNODB CHARSET=utf8; + +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`) +)ENGINE=INNODB CHARSET=utf8; + +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 "课程成绩高的学生的信息(学号、姓名、性别、出生日期)及课程分数 + +with a as (select score,student_id from score where course_id=1), + b as (select score,student_id from score where course_id=2), + c as (select * from student) + select a.student_id,a.score,c.* from a,b,c where a.student_id=b.student_id and a.score>b.score and a.student_id=c.student_id; + +-- 2. 查询同时参与" 1 "课程和" 2 "课程考试的学生信息 + +select * from student where student_id in +(with a as(select * from score where course_id=1), + b as (select * from score where course_id=2) +select a.student_id from a,b where a.student_id=b.student_id); + +-- 3. 查询存在" 1 "课程但可能不存在" 2 "课程的情况(不存在时显示为 null ) + +select * from (select * from score where course_id=1) a left join (select student_id from score where course_id =2) b on a.student_id=b.student_id where b.student_id is null; + +-- 4. 查询不存在" 1 "课程但存在" 2 "课程的情况 + +select b.student_id,b.score from (select * from score where course_id=1) a right join (select * from score where course_id =2) b on a.student_id=b.student_id where a.student_id is null; +-- 5. 查询平均成绩大于等于 60 分的同学的学生编号、姓名和平均成绩 + +select a.student_id 学生编号,s.student_name 姓名,a.av 平均成绩 from (select distinct student_id,ceil(avg(score)) av from score group by student_id having avg(score) >=60) a left join student s on a.student_id=s.student_id ; +-- 6. 查询在成绩表存在成绩的学生信息 + +select * from student where student_id in(select distinct student_id from score); +-- 7. 查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩(没成绩的显示为 null ) + +select s.student_id 学生编号,s.student_name 学生姓名,a.cou 选课总数,a.su 所有课程的总成绩 from (select student_id,count(course_id) cou,sum(score) su from score group by student_id) a right join student s on a.student_id=s.student_id; + +-- 8. 查询「李」姓老师的数量 + +select count(teacher_name),teacher_name from teacher where teacher_name like '李%' group by teacher_name ; + +-- 9. 查询学过「李白」老师授课的同学的信息 + +select teacher_id from teacher where teacher_name ='李白'; +select * from (select * from score where course_id =(select teacher_id from teacher where teacher_name ='李白')) a left join student s on a.student_id=s.student_id; + +-- 10. 查询没有学全所有课程的同学的信息 + +select count(course_id) from course; +select * from student where student_id not in(select student_id from score group by student_id having count(course_id) =(select count(course_id) from course)); +-- 先查出所有学完课程的同学ID,取反,就可以知道没有学完课程的同学id + +-- 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)); + +-- 12. 查询和" 1 "号的同学学习的课程完全相同的其他同学的信息 + +select s.* from student s,(select student_id,group_concat(course_id) cc from score group by student_id) a where s.student_id=a.student_id and a.cc in(select group_concat(course_id) from score group by student_id having student_id =1 ); + +-- 13. 查询没学过「李白」老师讲授的任一门课程的学生姓名 + +select * 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. 查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩 + +select c.student_id,t.student_name,group_concat(c.score),avg(c.score) from score c,student t where c.student_id=t.student_id and c.score<60 group by c.student_id; + +-- 15. 检索" 1 "课程分数小于 60,按分数降序排列的学生信息 + +select s.student_id,t.*,s.score from score s,student t where s.student_id=t.student_id and s.course_id =1 and s.score<60 order by s.score desc; + +-- 16. 按平均成绩从高到低显示所有学生的所有课程的成绩以及平均成绩 + +select student_id,avg(score),group_concat(score) from score group by student_id order by avg(score) desc; + +-- 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 S.course_id, + C.course_name, + MAX(score) 最高分, + MIN(score) 最低分, + AVG(score) 平均分, + CONCAT(ROUND(100 * SUM(CASE WHEN score >= 60 and score < 70 then 1 else 0 end) / COUNT(score), 2), '%') 及格率, + CONCAT(ROUND(100 * SUM(CASE WHEN score >= 70 and score < 80 then 1 else 0 end) / COUNT(score), 2), '%') 中等率, + CONCAT(ROUND(100 * SUM(CASE WHEN score >= 80 and score < 90 then 1 else 0 end) / COUNT(score), 2), '%') 优良率, + CONCAT(ROUND(100 * SUM(CASE WHEN score >= 90 then 1 else 0 end) / COUNT(score), 2), '%') 优秀率 +FROM score S + LEFT JOIN Course C on S.course_id = C.course_id +GROUP BY S.course_id, C.course_name; + +-- 19. 按各科成绩进行排序,并显示排名,Score 重复时保留名次空缺 + +select course_id,score,rank() over(partition by course_id order by score) from score; + + +-- 20. 按各科成绩进行排序,并显示排名,Score 重复时合并名次 + +select course_id,score,dense_rank() over(partition by course_id order by score) from score; + +-- 21. 查询学生的总成绩,并进行排名,总分重复时保留名次空缺 + +select rank() over(order by al),al 总成绩 from (select distinct student_id,sum(score) over(partition by student_id) al from score) a; + +-- 22. 查询学生的总成绩,并进行排名,总分重复时不保留名次空缺 + +select dense_rank() over(order by al),al 总成绩 from (select distinct student_id,sum(score) over(partition by student_id) al from score) a; + +-- 23. 统计各科成绩各分数段人数:课程编号,课程名称,[100-85],[85-70],[70-60],[60-0] 及所占百分比 + + +select c.course_id 课程编号, + c.course_name 课程名称, + count(s.course_id) 参考总人数, + concat(round(100 * sum(case when s.score between 0 and 60 then 1 else 0 end) / count(s.score), 2), + '%') '[0,60]占比', + concat(round(100 * sum(case when s.score between 0 and 60 then 1 else 0 end) / count(s.score), 2), + '%') '[0,60]占比', + concat(round(100 * sum(case when s.score between 61 and 70 then 1 else 0 end) / count(s.score), 2), + '%') '[70-60]占比', + concat(round(100 * sum(case when s.score between 71 and 85 then 1 else 0 end) / count(s.score), 2), + '%') '[85-70]占比', + concat(round(100 * sum(case when s.score between 86 and 100 then 1 else 0 end) / count(s.score), 2), + '%') '[100-85]占比' +from course c + left join score S on c.course_id = S.course_id +group by c.course_id, c.course_name; + + + +-- 24. 查询各科成绩前三名的记录 + +select * from (select course_id,score,row_number() over(partition by course_id order by score desc) 排名 from score) pm where pm.排名<=3; + +-- 25. 查询每门课程被选修的学生数 + +select course_id,count(student_id) from score group by 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(course_id) =2); + +-- 27. 查询男生、女生人数 + +select gender,count(student_id) from student group by gender; + +-- 28. 查询名字中含有「风」字的学生信息 + +select * from student where student_name like '%风%'; + +-- 29. 查询同名同性学生名单,并统计同名人数 + +select student_name,count(student_id) from student group by student_name having count(student_id)>=2; + +-- 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 asc; + +-- 32. 查询平均成绩大于等于 85 的所有学生的学号、姓名和平均成绩 + +select s.student_id,t.student_name,avg(s.score) from score s,student t where s.student_id=t.student_id group by s.student_id having avg(s.score)>=85; + +-- 33. 查询课程名称为「数学」,且分数低于 60 的学生姓名和分数 + +select s.student_name,a.score from student s,(select student_id,score from score where course_id=(select course_id from course where course_name ='数学') and score<60) a where s.student_id=a.student_id; + +-- 34. 查询所有学生的课程及分数情况(存在学生没成绩,没选课的情况) + +select t.student_id,s.course_id,s.score from score s right join student t on s.student_id=t.student_id; + +-- 35. 查询任何一门课程成绩在 70 分以上的姓名、课程名称和分数 ------------------- + +select * from student s,(select student_id,min(score) from score group by student_id having min(score)>70) a where s.student_id=a.student_id ; + + +select st.*,s.student_id,min(s.score) from teacher t ,course c,score s,student st where t.teacher_id=c.teacher_id and c.course_id=s.course_id and s.student_id=st.student_id group by s.student_id having min(s.score)>70; + + +select * from score where score in(select min(score) from score group by student_id having min(score)>70); + +-- 36. 查询不及格的课程 + +select * from course where course_id in(select distinct course_id from score where score <60) ; + +-- 37. 查询课程编号为 1 且课程成绩在 80 分以上的学生的学号和姓名 + +select student_id,student_name from student where student_id in(select student_id from score where course_id=1 and score >=80); + +-- 38. 求每门课程的学生人数 + +select course_id,count(student_id) from score group by course_id; + +-- 39. 成绩不重复,查询选修「张三」老师 所授课程的学生中,成绩最高的学生信息及其成绩(没有张三老师,我换了一个老师高斯) + +select * from teacher t ,course c,score s,student st where t.teacher_id=c.teacher_id and c.course_id=s.course_id and s.student_id=st.student_id and t.teacher_name='高斯' order by s.score desc limit 1; +-- 或者 + +select * from (select t.*,s.*,row_number() over(order by s.score desc) 排名 from teacher t ,course c,score s,student st where t.teacher_id=c.teacher_id and c.course_id=s.course_id and s.student_id=st.student_id and t.teacher_name='高斯') pm ,student ss where pm.student_id=ss.student_id and pm.排名=1; + +-- 40. 成绩有重复的情况下,查询选修「张三」老师所授课程的学生中,成绩最高的学生信息及其成绩(没有张三老师,我换了一个老师李白) + +select * from (select t.*,s.*,rank() over(order by s.score desc) 排名 from teacher t ,course c,score s,student st where t.teacher_id=c.teacher_id and c.course_id=s.course_id and s.student_id=st.student_id and t.teacher_name='李白') pm ,student ss where pm.student_id=ss.student_id and pm.排名=1; + + +-- 41. 查询不同课程成绩相同的学生的学生编号、课程编号、学生成绩 + +select distinct s.student_id ,s.course_id,s.score from score s,score c where s.course_id!=c.course_id and s.score=c.score; + +-- 42. 查询每门功成绩最好的前两名 + +select * from (select t.*,s.course_id,row_number() over(partition by s.course_id order by s.score desc) 排名 from score s , student t where s.student_id=t.student_id) b where b.排名<= 2; +-- 先弄排名,再对排名进行筛选 + +-- 43. 统计每门课程的学生选修人数(超过 5 人的课程才统计)。 + +select course_id,count(student_id) from score group by course_id having count(student_id)>5; + +-- 44. 检索至少选修两门课程的学生学号 +select student_id,count(course_id) from score group by student_id having count(course_id)>=2; + + +-- 45. 查询选修了全部课程的学生信息 + +select * from student where student_id in(select student_id from score group by student_id having group_concat(course_id) =(select group_concat(course_id) from course)); + +-- 46. 查询各学生的年龄,只按年份来算 + +select student_name,timestampdiff(year,birthday,now()) from student ; + +-- 47. 按照出生日期来算,当前月日 < 出生年月的月日则,年龄减一 + +select student_name,floor(datediff(now(),birthday)/365)-case when datediff(curdate(),365)