赞
踩
mysql> select sum(s_score) from score group by c_id having c_id=‘02’;
select distinct a.s_id,a.s_name
from student a
left join score b on a.s_id=b.s_id
where b.s_score<60 or a.s_id not in (select s_id from score);
(1) 查询课程成绩小于60分的学生的学号
(2) 将步骤1的查询结果和student表联表查询出姓名,同时考虑没有考试成绩的学生
select a.s_id,a.s_name
from student a
left join score b on a.s_id=b.s_id
group by a.s_id,a.s_name
having count(b.c_id)<(select count(c_id) from course);
select distinct a.s_id,a.s_name
from student a
right join score b on a.s_id=b.s_id
where b.c_id in (select c_id from score where s_id=‘01’) and a.s_id !=‘01’;
(1) 查询至少有一门课与学号为“01”的学生所学课程相同的学生的学号
(2) 根据步骤1的结果与student表联表查询出姓名,同时排除学号1的学生
select s_id
from score where s_id!=‘01’
group by s_id
having count(s_id) = (select count(*) from score where s_id=‘01’);
select s_id,s_name
from student
where s_id not in (select a.s_id
from score a
join course b on a.c_id=b.c_id
join teacher c on b.t_id=c.t_id
where c.t_name=‘张三’);
select a.s_id,a.s_name,avg(s_score) avg_score
from student a
join score b on a.s_id=b.s_id
where b.s_score<60
group by s_id
having count(b.s_id)>=2;
(1) 求出每个学生成绩不及格的课程数量及平均成绩
(2) 利用步骤1的结果与student表联表查询出学生姓名,其中课程成绩不及格数量要大于等于2门
select a.s_id,a.s_name
from student a
join score b on a.s_id=b.s_id
where b.s_score<60 and b.c_id=‘01’
order by b.s_score desc;
select a.s_id,a.s_score,b.avg_score
from score a
left join (select s_id, avg(s_score) avg_score
from score
group by s_id) b
on a.s_id =b.s_id
order by avg_score desc;
注意:上述虽然表达出了题目的意思,但是最好以横排形式展示,可以使用select嵌套子查询
方法2 :
select s_id,s_name,
(select s_score from score where score.s_id=student.s_id and c_id=‘01’) 01_score,
(select s_score from score where score.s_id=student.s_id and c_id=‘02’) 02_score,
(select s_score from score where score.s_id=student.s_id and c_id=‘03’) 03_score,
(select avg(s_score) from score where score.s_id=student.s_id) avg_score
from student
order by avg_score desc;
使用select子查询嵌套查询时,需要将有歧义的字段区分出来比如: score.s_id=student.s_id
(1) 查询各科成绩最高分、最低分和平均分
select a.c_id,a.c_name, avg(b.s_score) avg_score,min(b.s_score) min_score,max(b.s_score) max_score
from course a j
oin score b on a.c_id=b.c_id
group by b.c_id;
(2) 每一个科目的及格率,中等率,优良率,优秀率需要使用select后嵌套子查询获得
select
b.c_id,b.c_name, avg(a.s_score) avg_score,min(a.s_score) min_score,max(a.s_score) max_score,
– 注意:这里相当于将 score a和course b联表查询后再与score这张表联表查询
(select count() from score where c_id=a.c_id and s_score>=60)/count() ‘及格率’,
(select count() from score where c_id=a.c_id and s_score between 70 and 80)/count() ‘中登率’,
(select count() from score where c_id=a.c_id and s_score between 80 and 90)/count() ‘优良率’,
(select count() from score where c_id=a.c_id and s_score>90) /count() ‘优秀率’
from score a
join course b
on a.c_id=b.c_id
group by a.c_id;
select a.s_id,a.s_name,(case when sum(b.s_score) is null then 0 else sum(b.s_score) end) sum_score
from student a
left join score b on a.s_id=b.s_id
group by b.s_id
order by sum_score desc;
select c_id,avg(s_score) avg_score
from score
group by c_id
order by avg_score desc;
select s_id,avg(s_score) avg_score,
row_number () over( order by avg(s_score) desc) row_num
from score
group by s_id;
序号函数:row_number() / rank() / dense_rank()
partition子句:窗口按照那些字段进行分组,窗口函数在不同的分组上分别执行。下面的例子就按照 c_id进行了分组。在每个 c_id上,按照order by的顺序分别生成从1开始的顺序编号。
order by子句:按照哪些字段进行排序,窗口函数将按照排序后的记录顺序进行编号。可以和partition子句配合使用,也可以单独使用。如果没有partition子句,则会按照所有score排序来生成序号。
– partition by c_id 按照c_id进行分组
– order by s_score desc 按照s_score倒叙排序
select c_id,s_id,row_number() over(partition by c_id order by s_score desc) num_row
from score;
select a.s_id,a.s_name,b.num_row
from student a
– 将select查询作为一张子表,与student表联表查询
join (select
s_id,
c_id,
row_number() over(partition by c_id order by s_score desc) num_row
from score) b on a.s_id=b.s_id
where b.num_row<=2;
select a.s_id,a.s_name,b.c_id,b.s_score,b.num_row
from student a
join (select
s_id,
c_id,
s_score,
row_number() over(partition by c_id order by s_score desc) num_row from score) b
on a.s_id=b.s_id
where b.num_row between 2 and 3;
select a.s_id,a.s_name,b.c_id,b.s_score,b.num_row
from student a
join (select
s_id,
c_id,
s_score,row_number() over(partition by c_id order by s_score desc) num_row
from score) b
on a.s_id=b.s_id
where b.num_row<=3;
select a.c_id,a.c_name,
(select count() from score where score.c_id=a.c_id and s_score<60) ‘小于60’,
(select count() from score where score.c_id=a.c_id and s_score between 60 and 70) ‘60-70’,
(select count() from score where score.c_id=a.c_id and s_score between 70 and 85) ‘70-85’,
(select count() from score where score.c_id=a.c_id and s_score between 85 and 100) ‘85-100’
from course a
join score b on a.c_id=b.c_id
group by c_id;
mysql> select c_id,count(c_id) count from score group by c_id;
select a.s_id,a.s_name,count(b.s_id) count
from student a
join score b on a.s_id=b.s_id
group by s_id
having count=2;
mysql> select count(s_sex) count_sex from student group by s_sex;
mysql> select * from student where s_name like ‘%周%’;
mysql> select s_id,s_name from student where year(s_birth)=1990;
select a.s_id,a.s_name,avg(b.s_score) avg_score
from student a
join score b on a.s_id=b.s_id
group by b.s_id
having avg_score>=85;
select c_id, avg(s_score) avg_score from score group by c_id order by avg_score,c_id desc;
select a.c_id,b.c_name,c.s_id,c.s_name,a.s_score
from score a
join course b on a.c_id=b.c_id
join student c on a.s_id=c.s_id
where a.s_score<60 and b.c_name=‘数学’;
select b.s_id,b.s_name,c.c_id,c.c_name,a.s_score
from score a
join student b on a.s_id=b.s_id
join course c on a.c_id=c.c_id ;
select c.s_name,b.c_name,a.s_score
from score a
join course b on a.c_id=b.c_id
join student c on a.s_id=c.s_id
where a.s_score>70;
mysql> select distinct c_id from score where s_score<60 order by c_id desc;
select a.s_id,a.s_name
from student a
join score b on a.s_id=b.s_id
where b.s_score>80 and b.c_id=03;
mysql> select c_id, count(*) count from score group by c_id;
select a.s_score,b.s_id,b.s_name
from score a
join student b on a.s_id=b.s_id
join course c on a.c_id=c.c_id
join teacher d on c.t_id=d.t_id
where d.t_name=‘张三’
order by a.s_score desc
limit 1;
方法1:利用group by将s_id,c_id,s_score都重复的去除掉
select a.s_id,a.c_id,a.s_score
from score a
join score b on a.s_score=b.s_score
where a.c_id!=b.c_id and a.s_id=b.s_id
group by a.s_id,a.c_id,a.s_score;
方法2: 利用distinct将s_id,c_id,s_score都重复的去除掉
select distinct a.s_id,a.c_id,a.s_score
from score a
join score b on a.s_score=b.s_score
where a.c_id!=b.c_id and a.s_id=b.s_id ;
mysql> select c_id, count(*) count from score group by c_id order by c_id asc;
select s_id,count(*) count from score group by s_id having count>=2;
方法1 :
select a.s_id,a.s_name,a.s_birth,a.s_sex
from student a
join score b on a.s_id=b.s_id
group by a.s_id,a.s_name,a.s_birth,a.s_sex
having count(b.s_id) =(select count(*) from course);
方法2 :
select * from student a
– 查询score表中s_id=student表中s_id的行数,即统计每个学生的选修课程数量
where (select count() from score where s_id = a.s_id)=(select count() from course)
– 时间差函数:timestampdiff
select TIMESTAMPDIFF(SECOND,“2020-02-27”,NOW()) – 31680773
select TIMESTAMPDIFF(MINUTE,“2020-02-27”,NOW()) – 528010
select TIMESTAMPDIFF(HOUR,“2020-02-27”,NOW()) – 8800
select TIMESTAMPDIFF(DAY,“2020-02-27”,NOW()) – 366
select TIMESTAMPDIFF(WEEK,“2020-02-27”,NOW()) – 52
select TIMESTAMPDIFF(MONTH,“2020-02-27”,NOW()) – 12
select TIMESTAMPDIFF(QUARTER,“2020-02-27”,NOW()) – 4
自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。
深知大多数Go语言工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则几千的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!
因此收集整理了一份《2024年Go语言全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Golang知识点,真正体系化!
由于文件比较大,这里只是将部分目录大纲截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且后续会持续更新
如果你觉得这些内容对你有帮助,可以添加V获取:vip1024b (备注Go)
一个人可以走的很快,但一群人才能走的更远。不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎扫码加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!
自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!**
因此收集整理了一份《2024年Go语言全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
[外链图片转存中…(img-zm4g1Byx-1713064190207)]
[外链图片转存中…(img-S57usDOY-1713064190208)]
[外链图片转存中…(img-pORypJZZ-1713064190208)]
[外链图片转存中…(img-6NWAe1tq-1713064190209)]
[外链图片转存中…(img-sDZf1rL1-1713064190209)]
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Golang知识点,真正体系化!
由于文件比较大,这里只是将部分目录大纲截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且后续会持续更新
如果你觉得这些内容对你有帮助,可以添加V获取:vip1024b (备注Go)
[外链图片转存中…(img-bEhhkew4-1713064190210)]
一个人可以走的很快,但一群人才能走的更远。不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎扫码加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。