前言:今天将为大家带来MySQL综合训练题目,该部分是基于之前的各节内容,所以基础薄弱的伙伴建议先把之前的文章看了再来续读该篇文章----祝你好运!
create table stu(s_id varchar(5),
s_name varchar(5),
s_sex varchar(1),
s_age int(3),s_day date);
insert into stu values
("001","李华","男",23,'1996-8-16'),
("002","王二","男",24,'1997-3-16'),
("003","赵敏","女",23,'1990-5-26'),
("004","张莹莹","女",22,'1995-2-16'),
("005","朱亚军","男",25,'1999-8-16'),
("006","马云","男",28,'1993-12-16');
create table scores (s_id varchar(5),
c_id varchar(3),score float);
insert into scores values
("001","01",135),
("005","01",120),
("003","01",110),
("002","01",90),
("005","02",140),
("001","02",125.5),
("004","02",100),
("006","02",90),
("002","03",102),
("005","03",100.6),
("001","03",100),
("003","03",95.6),
("004","03",83),
("003","02",80),
("006","03",79.5);
create table coure(
c_id varchar(2),t_id char(1),c_name varchar(10));
insert into coure values
("01","3","数据库原理"),
("02","2","统计学基础"),
("03","1","Python基础");
create table teacher(
t_id char(1),t_name varchar(5));
insert into teacher values
("1","邓博"),
("2","崔博"),
("3","汪院长");
分析:由题意可知,需要将三张表格进行连接,且条件为课程名称“python基础”且分数低于90,这里,利用‘表名’+‘.’+‘字段’那么表示其查询的字段为该表格下的字段,其代码为:
select stu.s_name,score from stu left join scores on
stu.s_id=scores.s_id left join coure on scores.c_id=coure.c_id
where score<90 and c_name="python基础";
# 结果
张莹莹 83
马云 79.5
select stu.s_id,stu.s_name,score from stu left join scores on
stu.s_id=scores.s_id where c_id=01 and score>=90; # 法一 表格连接
select stu.s_id,s_name from stu where s_id in
(select s_id from scores where c_id=01 and score>90);# 法二 子查询 该方法没有将分数查询下来
# 结果
001 李华 135
002 王二 90
003 赵敏 110
005 朱亚军 120
select stu.s_id,stu.s_name,count(*),sum(score) from stu
left join scores on stu.s_id=scores.s_id group by s_id;
# 结果
001 李华 3 360.5
002 王二 2 192
003 赵敏 3 285.5999984741211
004 张莹莹 2 183
005 朱亚军 3 360.5999984741211
006 马云 2 169.5
select stu.s_id,s_name,avg(score) from stu left join scores on
stu.s_id=scores.s_id group by s_id having avg(score)>=100;
# 结果
001 李华 120.16666666666667
005 朱亚军 120.1999994913737
select stu.s_id,s_name,score from stu left join scores on
stu.s_id=scores.s_id group by stu.s_id having avg(score)>=100;
select s_id,s_name from stu where s_id in# 法二 子查询 无分数
(select s_id from scores group by s_id having avg(score)>=100);
# 结果
001 李华 135
005 朱亚军 120
select stu.s_id,s_name,avg(score) from stu left join scores on
stu.s_id=scores.s_id where score<90 group by s_id having count(*)>=2;
#很明显没有这样的学生,我们以100分看看
select stu.s_id,s_name,avg(score) from stu left join scores on
stu.s_id=scores.s_id where score<100 group by s_id having count(*)>=2;
select stu.s_id,s_name from stu where s_id in #子查询
(select s_id from scores where score<100 group by s_id having count(*)>=2);
# 结果为:
003 赵敏 87.79999923706055
006 马云 84.75
select * from stu left join scores on stu.s_id=scores.s_id
where score<100 and c_id=01 order by score desc;
# 答案
002 王二 男 24 1997-3-16 002 01 90
select * from stu left join scores on stu.s_id=scores.s_id # 多表连接
left join coure on scores.c_id=coure.c_id left join teacher on
coure.t_id=teacher.t_id where teacher.t_name="汪院长" group by stu.s_id;
# 结果
001 李华 男 23 1996-8-16 001 01 135 01 3 数据库原理 3 汪院长
002 王二 男 24 1997-3-16 002 01 90 01 3 数据库原理 3 汪院长
003 赵敏 女 23 1990-5-26 003 01 110 01 3 数据库原理 3 汪院长
005 朱亚军 男 25 1999-8-16 005 01 120 01 3 数据库原理 3 汪院长
select * from stu where s_id in # 子查询
(select s_id from scores where c_id in
(select c_id from coure where t_id in
(select t_id from teacher where t_name="汪院长")));
# 答案
001 李华 男 23 1996-8-16
002 王二 男 24 1997-3-16
003 赵敏 女 23 1990-5-26
005 朱亚军 男 25 1999-8-16
分析:该题目比较难,建议仔细品味。题目要求显示学生课程号、成绩、平均成绩,最难的是也要求显示平均分和每位同学每科分数,对每个同学求平均分需要分组,但是分组后信息会丢失,所有课程的信息如何得来?看朱哥如何解决:首先将分组后求得平均分和对应的学号用一个表装,再将其左连接学生得分表,再排序一下就是答案,代码为:
select a.s_id,平均分,c_id,score from # 法一 子查询
(select s_id,round(avg(score),2)平均分 from scores group by s_id)a
left join scores on a.s_id=scores.s_id order by 平均分 desc;
# 结果为:
s_id 平均分 c_id score
005 120.20 01 120
005 120.20 03 100.6
005 120.20 02 140
001 120.17 01 135
001 120.17 03 100
001 120.17 02 125.5
002 96.00 03 102
002 96.00 01 90
003 95.20 02 80
003 95.20 01 110
003 95.20 03 95.6
004 91.50 03 83
004 91.50 02 100
006 84.75 02 90
006 84.75 03 79.5
# 该结果看起来很让人不爽,于是可以将其转换为二维表,以学号与课程号做行标题和列标题
select s_id,
round(sum(c_id='01')*score,2) '01', #转为二维表的方式
round(sum(c_id='02')*score,2) '02',
round(sum(c_id='03')*score,2) '03',
round(avg(score),2) 平均分
from scores group by s_id order by 平均分 desc;
# 结果为: ps 这样看是不是很爽呢?代码也很简单
#s_id 01 02 03 平均分
005 120.00 120.00 120.00 120.20
001 135.00 135.00 135.00 120.17
002 90.00 0.00 90.00 96.00
003 110.00 110.00 110.00 95.20
004 0.00 100.00 100.00 91.50
006 0.00 90.00 90.00 84.75
select stu.* from (select s_id,round(avg(score),2) 平均成绩 from
scores group by s_id order by 平均成绩 desc limit 3)aa ,stu where
stu.s_id=aa.s_id;
# 结果
001 李华 男 23 1996-8-16
002 王二 男 24 1997-3-16
005 朱亚军 男 25 1999-8-16
又到了大家最喜欢的轻松一刻了,很多朋友又问我小熊最近干嘛,但是呢我不太好意思说,我就简单带来他女朋友就是我嫂子的故事,这不前天嘛,她女朋友参加她同学的订婚宴,当时她为了表现出自己已经结婚了就一直把手伸出来,但竟然没有一个同学注意到,令她忿忿不平。到下午大家坐着聊天的时候,她突然站起来大声说:“哎呀,这里真热呀,我看我还是把戒指脱下来吧,热死我的手指头了…”看到这里相信大家也明白他两为什么走到一起了,哈哈…
今天到这里就结束了哟//每篇文章都有文末彩蛋–轻松一刻哟~加关注学习更多MySQL知识!谢谢观看,我是Jetuser-data
链接: [https://blog.csdn.net/L1542334210]
CSND:L1542334210
祝大家工作顺利!阖家欢乐!