mysql之子查询作业
Posted 我想回家
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了mysql之子查询作业相关的知识,希望对你有一定的参考价值。
#数据准备
drop table if exists class;
create table class(
class_no int(2) unsigned zerofill primary key auto_increment comment ‘班级编号‘,
class_name varchar(30) not null comment ‘班级名称‘
);
insert into class values(1, ‘培优班‘);
insert into class values(2, ‘普通班‘);
drop table if exists student;
create table student(
stu_no int(2) unsigned zerofill primary key auto_increment comment ‘学员编号‘,
stu_name varchar(30) not null comment ‘学员姓名‘,
stu_sex varchar(3) not null comment ‘学员性别‘,
stu_age tinyint(2) unsigned zerofill comment ‘学员年代‘,
grade double(5,2) zerofill comment ‘成绩‘,
class_no int(2) unsigned zerofill comment ‘所在班级编号‘,
foreign key(class_no) references class(class_no)
);
insert into student values(01, ‘李白‘, ‘男‘, 18, 60, 01);
insert into student values(02, ‘杜甫‘, ‘男‘, 20, 76, 01);
insert into student values(03, ‘张飞‘, ‘男‘, 32, 80, 02);
insert into student values(04, ‘韩信‘, ‘男‘, 26, 98, 02);
insert into student values(05, ‘了龙‘, ‘男‘, 27, 56, 02);
insert into student values(06, ‘大乔‘, ‘女‘, 17, 88, 01);
insert into student values(07, ‘小乔‘, ‘女‘, 16, 96, 01);
insert into student values(08, ‘小乔‘, ‘女‘, 16, 90, 01);
insert into student values(09, ‘关哥‘, ‘男‘, 32, 80, 02);
insert into student values(10, ‘刘备‘, ‘男‘, 36, 98, null);
alter table student drop foreign key `student_ibfk_1`;
***********************************************************************************************************************************
// 班级为null表示不在任何一个班级,所以将班级为null的学生除外
1: 查询出每一个班级的最低成绩分别是多少
select class_no,min(grade) from student where (class_no is not null) group by class_no;
2: 查询出每一个班级的人数是多少
select class_no,count(*) from student where (class_no is not null) group by class_no;
3: 查询出每一个班级的平均分是多少,需求是按平均分的降序排序
select class_no,avg(grade) as avg from student where(class_no is not null) group by class_no order by avg desc;
4: 查询出每一个班级的男学员与女学员的平均分分别是多少,按照平均分的降序排序
select class_no,stu_sex,avg(grade) as avg from student group by class_no,stu_sex order by avg desc;
5: 查询出每一个班级学生的成绩在80到95的人数
select class_no, count(*) from student where (class_no is not null) and (grade between 80 and 95) group by class_no;
6: 查询出平均分小于80的班级
select class_no,avggrade from (select class_no,avg(grade) as avggrade from student group by class_no) as temp where (class_no is not null) and avggrade < 80;(创建临时表)
mysql> select class_no,avg(grade) as avg from student group by class_no having avg < 80;
7: 查询出01号班级的平均成绩和02班级的总成绩(使用一条语句)---使用并和查询
(select class_no,concat("平均成绩:",avg(grade)) as ‘成绩‘ from student where class_no = 1) union (select class_no,concat("总成绩:",sum(grade)) as ‘成绩‘ from student where class_no = 2);
查询每个班级的平均成绩和总成绩:
select class_no, avg(grade), sum(grade) from student where (class_no is not null) group by class_no;
8: 查询出平均分最低的班级
// 多个虚表
//select class_no from (select class_no,avg(grade) as grade from student where (class_no is not null) group by class_no) as temp where grade = (select min(t1_grade) from (select avg(grade) as t1_grade from student group by class_no) as temp1);
select class_no,avg(grade) from student group by class_no order by avg(grade) asc limit 1;
select class_no,avg(grade) as avg from student group by class_no having avg = (select avg(grade) from student group by class_no order by avg(grade) asc limit 1);
9: 查询出学号为4,8,9的学生
select * from student where stu_no in (4,8,9);
10: 查询出每一个班级中成绩小于平均分的学员
select * from student,(select class_no as cno ,avg(grade) as agrade from student where (class_no is not null) group by class_no) as temp where grade < agrade and class_no = cno;
以上是关于mysql之子查询作业的主要内容,如果未能解决你的问题,请参考以下文章