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;
轉載于:https://www.cnblogs.com/wadmwz/p/7612604.html
總結
以上是生活随笔為你收集整理的mysql之子查询作业的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: [JLOI 2012]树
- 下一篇: StringBuffer笔记