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之子查询作业的更多相关文章
- mysql in 子查询 效率慢 优化(转)
mysql in 子查询 效率慢 优化(转) 现在的CMS系统.博客系统.BBS等都喜欢使用标签tag作交叉链接,因此我也尝鲜用了下.但用了后发现我想查询某个tag的文章列表时速度很慢,达到5秒之久! ...
- MySQL 行子查询(转)
MySQL 行子查询 行子查询是指子查询返回的结果集是一行 N 列,该子查询的结果通常是对表的某行数据进行查询而返回的结果集. 一个行子查询的例子如下: SELECT * FROM table1 WH ...
- MySQL FROM 子查询
FROM 子句中的子查询 MySQL FROM 子查询是指 FROM 的子句作为子查询语句,主查询再到子查询结果中获取需要的数据.FROM 子查询语法如下: SELECT ... FROM (subq ...
- MySQL 表子查询
MySQL 表子查询 表子查询是指子查询返回的结果集是 N 行 N 列的一个表数据. MySQL 表子查询实例 下面是用于例子的两张原始数据表: article 表: aid title conten ...
- MySQL 行子查询
MySQL 行子查询 行子查询是指子查询返回的结果集是一行 N 列,该子查询的结果通常是对表的某行数据进行查询而返回的结果集. 一个行子查询的例子如下: SELECT * FROM table1 WH ...
- Mysql in子查询中加limit报错
Mysql in子查询中加limit报错 select id from aa where id in ( select id from bb limit 10 ); 改写成 SELECT id FRO ...
- MySQL(五) —— 子查询
子查询(SubQuery)是指出现在其他SQL语句内的SELECT语句. 如: SELECT * FROM t1 WHERE col1 = (SELECT col2 FROM t2); 其中 SELE ...
- MySql优化子查询
用子查询语句来影响子查询中产生结果rows的数量和顺序. For example: SELECT * FROM t1 WHERE t1.column1 IN (SELECT column1 FROM ...
- Mysql的子查询相关知识,少但是精
Mysql子查询 概念分析: 根据相关性分: (1)不相关子查询:一条Sql语句中含有多条SELECT语句,先执行子查询,再执行外查询,子查询可对立运行 关键字:(1)先子查询,再外查询 (2)可以对 ...
随机推荐
- Python下载图片小程序
欢迎大侠们指正批评 思路: 1.引入相关的python文件(import re import urllib) 2.读取对应网页的html文件(使用 urllib) def getHtml(url): ...
- linux下文件的复制、移动与删除命令为:cp,mv,rm
一.文件复制命令cp 命令格式:cp [-adfilprsu] 源文件(source) 目标文件(destination) cp [option] source1 source2 sour ...
- @Cacheable的实现原理
如果你用过Spring Cache,你一定对这种配置和代码不陌生: <cache:annotation-driven cache-manager="cacheManager" ...
- swift textview禁止用户使用复制粘贴
//自定义一个TextView class Own_TextView: UITextView { override func caretRect(for position: UITextPositio ...
- Beta阶段总结分析报告
1 讨论照片 2 Postmortem结果 二手交易平台项目Postmortem结果 整理:程环宇 设想和目标 1. 我们的软件要解决什么问题?是否定义得很清楚?是否对典型用户和典型场景有 ...
- Python 线程复习
修改全局变量,设立flag来避免线程间数据冲突,低效率版 from threading import Thread import time g_num=0 g_flag = 1 def test1() ...
- 201421123042 《Java程序设计》第14周学习总结
1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结与数据库相关内容. 答: 2. 使用数据库技术改造你的系统 2.1 简述如何使用数据库技术改造你的系统.要建立什么表?截图你的表设计 ...
- 怎么去理解JAVA中类与对象的关系
首先要明确,在现实生活中,每一个物体都有自己的基本特征,专业一点也可以说成是属性有些甚至还有一定的行为.例如 汽车的特征:有车门.有轮胎.颜色各一等等,行为:有行驶,开车门,开车灯,等等.有这些属性和 ...
- bzoj千题计划288:bzoj1876: [SDOI2009]SuperGCD
http://www.lydsy.com/JudgeOnline/problem.php?id=1876 高精压位GCD 对于 GCD(a, b) a>b 若 a 为奇数,b 为偶数,GCD ...
- c语言中宏定义和常量定义的区别
他们有共同的好处就是"一改全改,避免输入错误"哪两者有不同之处吗?有的. 主要区别就在于,宏定义是在编译之前进行的,而const是在编译阶段处理的 宏定义不占用内存单元而const ...