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;

时间: 2024-11-18 13:50:13

mysql之子查询作业的相关文章

MySQL FROM 子查询

FROM 子句中的子查询 MySQL FROM 子查询是指 FROM 的子句作为子查询语句,主查询再到子查询结果中获取需要的数据.FROM 子查询语法如下: SELECT ... FROM (subquery) AS name ... 子查询会生成一个临时表,由于 FROM 子句中的每个表必须有一个名称,因此 AS name 是必须的.FROM 子查询也称为衍生数据表子查询. FROM 子查询实例 table1: s1 s2 1 5 2 12 3 20 FROM 子查询 SQL 如下: SELE

MySQL 表子查询

MySQL 表子查询 表子查询是指子查询返回的结果集是 N 行 N 列的一个表数据. MySQL 表子查询实例 下面是用于例子的两张原始数据表: article 表: aid title content uid 1 文章1 文章1正文内容... 1 2 文章2 文章2正文内容... 1 3 文章3 文章3正文内容... 2 4 文章4 文章4正文内容... 3 blog 表: bid title content uid 1 日志1 日志1正文内容... 1 2 文章2 文章2正文内容... 1

生产库中遇到mysql的子查询

使用过oracle或者其他关系数据库的DBA或者开发人员都有这样的经验,在子查询上都认为数据库已经做过优化,能够很好的选择驱动表执行,然后在把该经验移植到mysql数据库上,但是不幸的是,mysql在子查询的处理上有可能会让你大失所望,在我们的生产系统上就由于碰到了这个问题: select  i_id, sum(i_sell) as i_sell from table_data where i_id in (select i_id from table_data where Gmt_create

mysql in 子查询 效率慢 优化(转)

现在的CMS系统.博客系统.BBS等都喜欢使用标签tag作交叉链接,因此我也尝鲜用了下.但用了后发现我想查询某个tag的文章列表时速度很慢,达到5秒之久!百思不解(后来终于解决),我的表结构是下面这样的,文章只有690篇. 文章表article(id,title,content)标签表tag(tid,tag_name)标签文章中间表article_tag(id,tag_id,article_id)其中有个标签的tid是135,我帮查询标签tid是135的文章列表用以下语句时发现速度好慢,我文章才

MySql优化子查询

用子查询语句来影响子查询中产生结果rows的数量和顺序. For example: SELECT * FROM t1 WHERE t1.column1 IN (SELECT column1 FROM t2 ORDER BY column1); SELECT * FROM t1 WHERE t1.column1 IN (SELECT DISTINCT column1 FROM t2); SELECT * FROM t1 WHERE EXISTS (SELECT * FROM t2 LIMIT 1)

Mysql的子查询相关知识,少但是精

Mysql子查询 概念分析: 根据相关性分: (1)不相关子查询:一条Sql语句中含有多条SELECT语句,先执行子查询,再执行外查询,子查询可对立运行 关键字:(1)先子查询,再外查询 (2)可以对立运行,即可以单独运行子查询,对外查询不干扰 (2)相关子查询:子查询不能独立运行,并且先运行外查询,再运行子查询 关键字:(1)先外查询,再子查询 (2)子查询不能独立运行 例如: (1)不相关子查询 --查询[和CLARK同一部门且比他工资低的]雇员名字和工资. -- 第一步:先查询CLARK部

mysql in子查询的优化

项目遇到一个MySQL查询特别慢的语句: SELECT * FROM ( SELECT DISTINCT t.vc_date, t.c_bankno, t.vc_bankacco, t.vc_moneytype, t.en_totalbala FROM tbankaccobala t WHERE 1 = 1 AND t.id IN ( -- 这个查询需要3s: SELECT SUBSTRING_INDEX(GROUP_CONCAT(id ORDER BY d_importtime DESC),

mysql之子查询、视图、事务及pymysql等(待修改)

联合分组 # 数据来源:在单表emp下 # 联合分组:按多个字段综合结果进行分组 # 按 area与port组合后的结果进行分组,只有组合后的结果还一致,才认为是一组 select group_concat(name),area,port from emp group by area,port; 子查询 # 增:insert into 表 select子查询 # 删:delete from 表 条件是select子查询(表不能与delete表相同) # 查:select 字段 from 表 条件

MySQL:子查询

之前介绍的都是简单查询,MySQL还支持子查询,常见的子查询用法有在where子句IN操作符中使用和计算字段中使用,下面分别举例说明. 用到的表参考https://www.cnblogs.com/july23333/p/11763375.html 1.where子句中 例:列出订购物品TNT2的所有客户信息 客户表中存有客户信息,订单表中存有订单号和客户ID,购买物品存在订单商品表中.我们从内至外依此写出查询语句在嵌套进外一层. 购买TNT2的订单号: SELECT order_num FROM