From 4b1e92a434fa7feade7b110ee04299c2d758896b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=82=B9=E4=BA=A8=E4=BC=9F?= <529310475@qq.com> Date: Mon, 23 Oct 2023 11:11:58 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8D=81=E6=9C=88=E4=BA=8C=E5=8D=81=E4=BA=8C?= =?UTF-8?q?=E5=8F=B7=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...32\344\272\224\345\215\201\351\242\230.md" | 399 ++++++++++++++++++ 1 file changed, 399 insertions(+) create mode 100644 "11 \351\202\271\344\272\250\344\274\237/\346\227\240\346\225\214\346\227\213\351\243\216\344\270\215\344\274\232\345\201\232\344\272\224\345\215\201\351\242\230.md" diff --git "a/11 \351\202\271\344\272\250\344\274\237/\346\227\240\346\225\214\346\227\213\351\243\216\344\270\215\344\274\232\345\201\232\344\272\224\345\215\201\351\242\230.md" "b/11 \351\202\271\344\272\250\344\274\237/\346\227\240\346\225\214\346\227\213\351\243\216\344\270\215\344\274\232\345\201\232\344\272\224\345\215\201\351\242\230.md" new file mode 100644 index 0000000..65ac126 --- /dev/null +++ "b/11 \351\202\271\344\272\250\344\274\237/\346\227\240\346\225\214\346\227\213\351\243\216\344\270\215\344\274\232\345\201\232\344\272\224\345\215\201\351\242\230.md" @@ -0,0 +1,399 @@ +## 笔记 + +窗口函数:的作用类似于在查询中对数据进行分组,不同的是,分组操作会把分组的结果聚合成一条记录,而窗口函数是将结果置于每一条数据记录中。 + +``` + -- 语法 + 函数 over (partition by 字段名 / order by 字段名 asc / desc) +``` + +partition by :指定窗口函数按照那些字段进行分组,分组后,窗口函数可以在每个分组中分别执行。 + +order by:指定窗口函数按照那些字段进行排序。 + +窗口函数总体上可以分为序号函数,分布函数,前后函数,首尾函数和其他函数。 + + + +### 函数复习 + +``` +数学函数 +-- 绝对值abs() +select abs(-4) -- 4 + +-- 向上取整 ceil(值) +select ceil(1.5) -- 2 + +-- 向下取整 floor(值) +select floor(1.5) -- 1 + +-- 随机数 rand() +select floor(rand()*100) + +-- 四舍五入,截取 round(值,保留点后n位) +select round(1.6666); +select round(1.6666,2); + +-- 截取 truncate(值,保留点后n位) + + +字符串函数 + +-- 字符个数 +select char_length('abc') -- 3 +select char_length('软件工程') -- 4 + +-- 拼接字符串 +select concat('你','好','帅哥') +select concat_ws(',','你','好','帅哥') -- 第一个字符为分隔符 + +-- 去空 +select ltrim(' aaa'); +select rtrim('aaa '); +select trim(' aaa ') + +-- 截取 +select substr('hello',2,3); + +-- 获取子串在字符串的位置 +select position('a' in 'abc'); + +-- 替换 +select replace('aaabbbccc','a','1'); + + + +日期函数 +-- 获取当前日期 +select curdate(); + +-- 获取当前时间 +select curtime(); + +-- 获取当前日期时间 +select now(); + +-- 从日期字符串获取日期 +select date('2000-02-02'); +select date('2000-02-02 12:12:12'); -- 只获取日期 + +-- 天数差 +select datediff('2001-02-02','2000-02-02'); -- 前减后 + +-- 指定差 +select timestampdiff(day,'2001-02-02','2000-02-02'); -- 后减前 + +-- 日期减法 +select subdate('2001-02-02',interval 2 day); + +-- 日期加法 +select adddate('2001-02-02',interval 2 day); + +-- 获取某日期中的值 +select year('2001-02-02'); +select day('2001-02-02'); +select dayofyear('2001-02-02'); +select dayofweek('2001-02-02'); + +-- 获取该月最后一天 +select last_day('2001-02-02'); + +-- 日期格式 +select date_format('2001-02-02','%Y') + + +控制流函数 +-- if(表达式,值1,值2) true返回值1,false返回值2 +select if(1<2,1,2); + +-- ifnull(值1,值2) 如果值1不为null,返回值1,否则返回值2 +select ifnull(null,2); +select ifnull(1,2); + +-- isnull(表达式) 返回1或0 +select isnull(null); +select isnull(''); + +-- nullif(值1,值2) 比较两个值(字符串),如果当等返回null,否则返回值1 +select nullif(25,25); + +``` + +``` +Mysql时间查询语句 +做日期对比: +select * from 表名 where DATE_FORMAT(时间字段,’%Y-%m-%d’) = 年-月-日; +如: +select * from student where DATE_FORMAT(time,’%Y-%m-%d’) = 2020-11-20; +获取当前时间: +使用now() +select * from 表名 where DATE_FORMAT(时间字段,’%Y-%m-%d’)=DATE_FORMAT(NOW(),’%Y-%m-%d’); +如: +select * from student where DATE_FORMAT(time,’%Y-%m-%d’) = DATE_FORMAT(NOW(),’%Y-%m-%d’); +也可以只判断年份: +select * from 表名 where DATE_FORMAT(时间字段,’%Y’)=年; +如: +select * from student where DATE_FORMAT(time,’%Y’) = 2020; +还可以判断月份等,方法跟年份那样。 +%Y显示年份4位 +%y显示年份2位 +%M显示月份为英文 +%b 显示月份位英文缩写(Jan~Dec) +%m显示月份为数字(01~12) +%c显示月份数字(1~12) +%W显示星期英文 +%a显示缩写星期英文 +%d显示月份天数(00~31) +%e显示月份天数(0~31) +%j显示 一年中的天数(001~366) +%H显示小时(00~23) +%k显示小时(0~23) +%h显示小时(01~12) +%i显示分钟, 数字(00~59) +%r显示时间,12 小时(hh:mm:ss [AP]M) +%T显示时间,24 小时(hh:mm:ss) +%S显示秒(00~59) +%p显示AM或PM +%w显示一个星期中的天数(0=Sunday ……6=Saturday ) +%U显示星期(0……52), 这里星期天是星期的第一天 +%u显示星期(0……52), 这里星期一是星期的第一天 +``` + +## 作业 + + + +``` +## 题目: + +-- 1. 查询" 1 "课程比" 2 "课程成绩高的学生的信息(学号、姓名、性别、出生日期)及课程分数 +select c.*,a.score,b.score from + (select * from score where course_id = 1) a join + (select * from score where course_id = 2) b on + a.student_id = b.student_id join + student c on + b.student_id = c.student_id +where +a.score > b.score; +-- 2. 查询同时参与" 1 "课程和" 2 "课程考试的学生信息 +select c.* from + (select * from score where course_id = 1) a join + (select * from score where course_id = 2) b on + a.student_id = b.student_id join + student c on + b.student_id = c.student_id; +-- 3. 查询存在" 1 "课程但可能不存在" 2 "课程的情况(不存在时显示为 null ) +select * from +(select * from score where course_id = 1) a left join +(select * from score where course_id = 2) b on a.student_id = b.student_id where b.course_id is null; +-- 4. 查询不存在" 1 "课程但存在" 2 "课程的情况 +select * 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.course_id is null; +-- 5. 查询平均成绩大于等于 60 分的同学的学生编号、姓名和平均成绩 +select * from +(select distinct a.student_id,student_name,avg(score) over (partition by student_id) 平均成绩 from student a join score b on a.student_id= b.student_id) a +where 平均成绩 >= 60; +-- 6. 查询在成绩表存在成绩的学生信息 +select a.* from student a, +(select distinct student_id from score) b +where a.student_id = b.student_id; +-- 7. 查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩(没成绩的显示为 null ) +select + distinct a.student_id , + a.student_name , + count(course_id) over (partition by student_id) 选课总数, + sum(score) over (partition by student_id) 总成绩 + from student a left join score b + on a.student_id = b.student_id; +-- 8. 查询「李」姓老师的数量 +select count(teacher_name) from teacher where teacher_name like '李%'; +-- 9. 查询学过「李白」老师授课的同学的信息 +select a.* from student a , score b , course c,teacher d + where a.student_id = b.student_id + and b.course_id = c.course_id + and c.teacher_id = d.teacher_id + and d.teacher_name = '李白'; +-- 10. 查询没有学全所有课程的同学的信息 +SELECT t.* from score s,student t where s.student_id=t.student_id GROUP BY t.student_id HAVING count(s.course_id) > (SELECT count(course_id) from course); +-- select * from student a left join score b on a.student_id = b.student_id right join course c on b.course_id = c.course_id; + +-- 11. 查询至少有一门课与学号为" 1 "的同学所学相同的同学的信息 +select distinct a.* from student a left join score b on a.student_id = b.student_id + where course_id in + (select course_id from score where student_id = 1); +-- 12. 查询和" 1 "号的同学学习的课程完全相同的其他同学的信息 + + +-- 13. 查询没学过「李白」老师讲授的任一门课程的学生姓名 + +-- select * from student a +-- left join score b on a.student_id = b.student_id +-- left join course c on b.course_id = c.course_id +-- left join teacher d on c.teacher_id = d.teacher_id + +-- 14. 查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩 + +-- 15. 检索" 1 "课程分数小于 60,按分数降序排列的学生信息 +select b.* from +(select * from score where course_id = 1 and score < 60 order by score desc) a left join student b on a.student_id= b.student_id; +-- 16. 按平均成绩从高到低显示所有学生的所有课程的成绩以及平均成绩 +select * from +(select student_id 学生id,course_id 课程id,score 成绩,avg(score) over (partition by student_id) 平均成绩 from score) a +order by 平均成绩 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 * from +-- 19. 按各科成绩进行排序,并显示排名,Score 重复时保留名次空缺 +select student_id,course_name,score,row_number() over(partition by student_id order by score desc) 排名 + from score sco left join course c on sco.course_id = c.course_id ; +-- 20. 按各科成绩进行排序,并显示排名,Score 重复时合并名次 +select student_id,course_name,score,dense_rank() over(partition by student_id order by score desc) 排名 + from score sco left join course c on sco.course_id = c.course_id ; +-- 21. 查询学生的总成绩,并进行排名,总分重复时保留名次空缺 +select *,row_number() over(order by a.总分 desc) from + (select stu.student_id,student_name, sum(score) 总分 + from student stu left join score sco on stu.student_id = sco.student_id + group by stu.student_id order by 总分 desc) a; +-- 22. 查询学生的总成绩,并进行排名,总分重复时不保留名次空缺 +select *,rank() over(order by a.总分 desc) from + (select stu.student_id,student_name, sum(score) 总分 + from student stu left join score sco on stu.student_id = sco.student_id + group by stu.student_id order by 总分 desc) a; +-- 23. 统计各科成绩各分数段人数:课程编号,课程名称,[100-85],[85-70],[70-60],[60-0] 及所占百分比 +select sco.course_id,course_name, + count(score<60 or null) `[60-0]人数`, + count(score>=60 and score<=70 or null) `[70-60]人数`, + count(score >70 and score <=85 or null) `[85-70]人数`, + count(score>85 or null) `[100-85]人数`, + concat(round(count(score<60 or null)/count(sco.course_id)*100,2),'%') `[0-60]所占百分比`, + concat(round(count(score>=60 and score<=70 or null)/count(sco.course_id)*100,2),'%')`[60-70]所占百分比`, + concat(round(count(score >70 and score <=85 or null)/count(sco.course_id)*100,2),'%') `[70-85]所占百分比`, + concat(round(count(score>85 or null)/count(sco.course_id)*100,2),'%') `[100-85]所占百分比` + from score sco left join course c on sco.course_id = c.course_id + group by sco.course_id ; +-- 24. 查询各科成绩前三名的记录 +select * from + (select student_id,course_name,score,row_number() over(partition by sco.course_id order by score desc) 排名 + from score sco left join course c on sco.course_id = c.course_id) a where a.排名 < 4 +-- 25. 查询每门课程被选修的学生数 +select course_id,count(course_id) from score group by course_id; +-- 26. 查询出只选修两门课程的学生学号和姓名 +select * from student s where (select count(*) from score sc where s.student_id=sc.student_id)<>(select count(*) from course); +-- 27. 查询男生、女生人数 +SELECT count(gender) 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_name,avg(score) from score s LEFT JOIN course c on s.course_id=c.course_id GROUP BY c.course_id ORDER BY avg(score) desc,c.course_id asc; +-- 32. 查询平均成绩大于等于 85 的所有学生的学号、姓名和平均成绩 +SELECT s.student_id,s.student_name,avg(score) from student s LEFT JOIN score c on s.student_id=c.student_id GROUP BY s.student_id HAVING avg(score)>85; +-- 33. 查询课程名称为「数学」,且分数低于 60 的学生姓名和分数 +SELECT DISTINCT s.student_name,score from student s LEFT JOIN score c on s.student_id=c.student_id LEFT JOIN course o on c.course_id=o.course_id where course_name='数学' and score <60; +-- 34. 查询所有学生的课程及分数情况(存在学生没成绩,没选课的情况) +SELECT + s.student_id, + s.student_name, + c.course_name, + sc.score +FROM + score sc + RIGHT JOIN student s ON sc.student_id = s.student_id + LEFT JOIN course c ON sc.course_id = c.course_id +GROUP BY + s.student_id, + s.student_name, + c.course_name, + sc.score; +-- 35. 查询任何一门课程成绩在 70 分以上的姓名、课程名称和分数 +select s.student_name,c.course_name,sc.score from student s,score sc,course c where s.student_id=sc.student_id and sc.course_id=c.course_id and sc.score>70; +-- 36. 查询不及格的课程 +select distinct c.course_name from score s,course c where s.course_id=c.course_id and score<60; +-- 37. 查询课程编号为 1 且课程成绩在 80 分以上的学生的学号和姓名 +select s.student_id,s.student_name,sc.score from student s,score sc where s.student_id=sc.student_id and sc.course_id=1 and sc.score in (select score from score where score>=80); +-- 38. 求每门课程的学生人数 +select course_id,count(course_id) from score group by course_id; +-- 39. 成绩不重复,查询选修「张三」老师所授课程的学生中,成绩最高的学生信息及其成绩 +SELECT + s.* ,sc.score +FROM + student s, + score sc, + course c, + teacher t +WHERE + s.student_id = sc.student_id + AND sc.course_id = c.course_id + AND c.teacher_id = t.teacher_id + AND sc.score IN ( + SELECT + max( score ) + FROM + score sc, + course c, + teacher t + WHERE + sc.course_id = c.course_id + AND c.teacher_id = t.teacher_id + AND t.teacher_name = '高斯' + ); +-- 40. 成绩有重复的情况下,查询选修「张三」老师所授课程的学生中,成绩最高的学生信息及其成绩 +SELECT + distinct s.* ,sc.score +FROM + student s, + score sc, + course c, + teacher t +WHERE + s.student_id = sc.student_id + AND sc.course_id = c.course_id + AND c.teacher_id = t.teacher_id + AND sc.score IN ( + SELECT + max( score ) + FROM + score sc, + course c, + teacher t + WHERE + sc.course_id = c.course_id + AND c.teacher_id = t.teacher_id + AND t.teacher_name = '高斯' + ); +-- 41. 查询不同课程成绩相同的学生的学生编号、课程编号、学生成绩 +select stu.student_id,course_id,score + from student stu left join score sco on stu.student_id = sco.student_id + where stu.student_id in + (select distinct s1.student_id from score s1,score s2 where s1.course_id != s2.course_id and s1.score = s2.score); +-- 42. 查询每门功成绩最好的前两名 +select * from + (select student_id,course_name,score,row_number() over(partition by sco.course_id order by score desc) 排名 + from score sco left join course c on sco.course_id = c.course_id ) a + where a.排名 <=2 +-- 43. 统计每门课程的学生选修人数(超过 5 人的课程才统计)。 +select course_id,count(course_id) from score GROUP BY course_id having count(course_id)>=5; +-- 44. 检索至少选修两门课程的学生学号 +select s.student_id,count(sc.course_id) from student s,score sc where s.student_id=sc.student_id group by s.student_id having count(sc.course_id)>2; +-- 45. 查询选修了全部课程的学生信息 +select s.*, count(sc.student_id) from student s,score sc where s.student_id=sc.student_id group by sc.student_id having count(sc.student_id)=3; +-- 46. 查询各学生的年龄,只按年份来算 +select year(now())-year(birthday) from student; +-- 47. 按照出生日期来算,当前月日 < 出生年月的月日则,年龄减一 +select student_name,timestampdiff(year,birthday,now()) 年龄 from student; +-- 48. 查询本周过生日的学生 +select * from student where weekday(student.birthday)=weekday(now()); +-- 49. 查询下周过生日的学生 +select * from student where weekday(student.birthday)=weekday(now())+1; +-- 50. 查询本月过生日的学生 +select * from student where month(student.birthday)=month(now()); +-- 51. 查询下月过生日的学生 +select * from student where month(birthday)=month(now())+1; +``` + -- Gitee