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`;
*******************************************************************************************************************************
1: [ group by ] 分组
需求: 查询出每一个班级最高成绩是多少
例: select class_no, max(grade) from student group by class_no; --先按class_no分组,然后再拿到每一组的最高成绩
例: select class_no, sum(grade) from student group by class_no; --查询每一个班级的总成绩
例: select class_no, min(grade) from student group by class_no; --查询每一个班级的最低成绩
例: select class_no, avg(grade) from student group by class_no; --查询每一个班级的平均成绩
例: select class_no, count(*) from student group by class_no; --查询每一个班级的人数
ps: group by一般要与max,min,avg等这些函数一起使用
例: select * from student group by class_no;
--可以进多重分组
需求: 查询出每一个班级男学生和女学生的最高成绩分别是多少
例: select class_no, stu_sex ,max(grade) from student group by class_no, stu_sex;
例: select class_no, stu_name, max(grade) from student group by class_no; --不能这样做
ps: 在要查询的字段集中,这些字段要么是包含在group by语句的后面的字段, 要么就是被包含在聚合函数中, 否则会报错
--还可以对满足条件的记录进行分组
select class_no, max(grade) from student where class_no is not null group by class_no;
*******************************************************************************************************************************
2: having
例: select * from student where class_no = 1 and stu_sex = '男';
例: select * from student having class_no = 1 and stu_sex = '男';
例: select class_no, max(grade) from student group by class_no having class_no is not null; --having
例: select class_no, max(grade) from student group by class_no where class_no is not null; --报错
ps: having需要跟在group by后面,而where不能跟在group by后面
*******************************************************************************************************************************
查询它可以配合5个字句来执行,查询到相应数据(where, order by ,limit, group by, having)
这个子句有一个顺序,需要按照顺序来写
select * from student [where] [group by] [having] [order by] [limit];
例: select class_no, stu_sex, avg(grade) from student where(class_no is not null) group by class_no,stu_sex having(stu_sex = '男') order by class_no desc limit 1;
*******************************************************************************************************************************
3: [ in ](集合运算符)
需求: 查询出学号是2,3,5的学生
例: select * from student where stu_no = 2 or stu_no = 3 or stu_no =5; --使用or运算符
例: select * from student where stu_no in (2,3,5); --使用in集合运算符
例: select * from student where class_no in (2, null); --in集合运算符查询不到null值
--还可以有not in
例: select * from student where stu_no not in (2,3,5);
例: select * from student where class_no not in (2, null); --查询结果为空
*******************************************************************************************************************************
4: 子查询
需求: 获取student表里成绩成绩最高的学员
例: select * from student order by grade desc limit 1; --这里不满足需求
例: select * from student where grade = max(grade); --报错
例: select * from student where grade = (select max(grade) from student);
ps: 在查询里边还有其它的查询,那么我们就把里边的查询叫做子查询,子查询需要括号包起来, 子查询可以有多个
ps: 子查询其实也就是一个查询,所以它返回的结果有以下几种情况(单一值),(一列),(一行或者一行多列),(多行多列)
根据返回值的情况不同,可以把子查询分为四种情况
1: 单一值(标量子查询)
需求: 获取student表里成绩成绩最高的学员
例: select * from student where grade = (select max(grade) from student);
2: 一列(列子查询)
例: select stu_age from student where grade < 95;
需求: 查询出成绩小于95的学员年龄
[ in ](集合运算符)
例: select * from student where stu_age in (select stu_age from student where grade < 95); --in在集合中存在的
例: select * from student where stu_age not in (select stu_age from student where grade < 95); --not in在集合中不存在的
[ any ]
例: select * from student where stu_age = any(select stu_age from student where grade < 95); -- =any等于集合中任意一个就行
例: select * from student where stu_age != any(select stu_age from student where grade < 95); -- !=any不满足集合中任意一个就行
[ all ]
例: select * from student where stu_age = all(select stu_age from student where grade < 95); -- =all等于集合中所有元素
例: select * from student where stu_age != all(select stu_age from student where grade < 95); -- !=不等于集合中的所有元素(意思就是集合中不存在的)
3: 一行(行子查询)
需求: 查询出同一班中相同成绩的 学生的姓名,班级,成绩;
select stu_name, class_no, grade from student where (class_no, grade) = (select class_no,grade from student group by class_no, grade having count(*) > 1);
ps: (class_no, grade)意思是临时构造成一个行,根据子查询到一行去比较;
4: 多行多列(表子查询)
需求: 查询出表中的stu_name,stu_sex的字段,要使用子查询
例: select * from (select stu_name, stu_sex from student) as stu;
ps:from后面需要跟一个表,如果是跟着是一个子查询得的一个临时表,那么你需要给这个字查询起一个加名;
mysql之查询的更多相关文章
- Linux下MySQL慢查询分析mysqlsla安装使用
说明: 操作系统:CentOS 5.X 64位 MySQL版本:mysql-5.5.35 MySQL配置文件:/etc/my.cnf MySQL 数据库存放目录:/data/mysql 实现目的:开启 ...
- MySQL的查询计划中ken_len的值计算
本文首先介绍了MySQL的查询计划中ken_len的含义:然后介绍了key_len的计算方法:最后通过一个伪造的例子,来说明如何通过key_len来查看联合索引有多少列被使用. key_len的含义 ...
- mysql的查询、子查询及连接查询
>>>>>>>>>> 一.mysql查询的五种子句 where(条件查询).having(筛选).group by(分组). ...
- MySQL慢查询日志总结
慢查询日志概念 MySQL的慢查询日志是MySQL提供的一种日志记录,它用来记录在MySQL中响应时间超过阀值的语句,具体指运行时间超过long_query_time值的SQL,则会被记录到慢查询日志 ...
- 【转】Mysql联合查询union和union all的使用介绍
Mysql的联合查询命令UNION和UNION ALL,总结了使用语法和注意事项,以及学习例子和项目例子,需要的朋友可以参考下 一.UNION和UNION ALL的作用和语法 UNION 用于合... ...
- mysql慢查询日志分析工具 mysqlsla(转)
mysql数据库的慢查询日志是非常重要的一项调优辅助日志,但是mysql默认记录的日志格式阅读时不够友好,这是由mysql日志记录规则所决定的,捕获一条就记录一条,虽说记录的信息足够详尽,但如果将浏览 ...
- Mysql慢查询和慢查询日志分析
Mysql慢查询和慢查询日志分析 众所周知,大访问量的情况下,可添加节点或改变架构可有效的缓解数据库压力,不过一切的原点,都是从单台mysql开始的.下面总结一些使用过或者研究过的经验,从配置以 ...
- [django/mysql] 使用distinct在mysql中查询多条不重复记录值的解决办法
前言:不废话.,直接进入正文 正文: 如何使用distinct在mysql中查询多条不重复记录值? 首先,我们必须知道在django中模型执行查询有两种方法: 第一种,使用django给出的api,例 ...
- MySQL 慢查询日志分析及可视化结果
MySQL 慢查询日志分析及可视化结果 MySQL 慢查询日志分析 pt-query-digest分析慢查询日志 pt-query-digest --report slow.log 报告最近半个小时的 ...
- mysql datetime查询异常
mysql datetime查询异常 异常:Value '0000-00-00 00:00:00' can not be represented as java.sql.Timestamp (2011 ...
随机推荐
- Spring Boot + Freemarker多语言国际化的实现
最近在写一些Web的东西,技术上采用了Spring Boot + Bootstrap + jQuery + Freemarker.过程中查了大量的资料,也感受到了前端技术的分裂,每种东西都有N种实现, ...
- 内核级线程(KLT)和用户级线程(ULT)
内核级线程(KLT)和用户级线程(ULT) tags: KLT ULT 内核级线程 用户级线程 引言:本文涉及到操作系统的内核模式和用户模式,如果不太懂的话,可以参看我的这篇文章内核模式和用户模式,其 ...
- 关于如何学习C语言
2016级计算机专业的C语言分为两个学期,第一学期是C语言(基础),第二学期是C语言(高级),在第一学期主要学习的内容是基本的数据类型,分支结构和循环结构,一维和二维数组,字符数组,函数.通过这学期独 ...
- Beta冲刺第四天
一.昨天的困难 没有困难. 二.今天进度 1.林洋洋:修复协作详情,日程详情日程类型显示纠正 2.黄腾达:修复管理者查看协作成员可以移除自己的问题,加入登录.注册表单按回车键就可直接完成操作的功能 3 ...
- alpha-咸鱼冲刺day7
一,合照 emmmmm.自然还是没有的. 二,项目燃尽图 三,项目进展 正在写登陆+注册ing 注册搞出来了!!!!!!!!QAQ(喜极而泣!!!!.jpg) 四,问题困难 数据流程大概是搞定了.不过 ...
- alpha-咸鱼冲刺day1
一,合照 emmmmm.自然是没有的. 二,项目燃尽图 三,项目进展 登陆界面随意写了一下.(明天用来做测试的) 把学姐给我的模板改成了自家的个人主页界面,侧边栏啥的都弄出来了(快撒花花!) 四,问题 ...
- c/cpp语言链表连接部分详解
核心代码: ①pTail->next = pNew; ②pNew->next = NULL; ③pTail = pNew; 设结构体名称为 struct ST: 注:方框代表分配的内存空间 ...
- DML数据操作语言之常用函数
所谓函数,就是输入某一值,得到相应的输出结果的功能.相当于一个加工厂,给了原料,最终产出成品. 其中原料 就是参数(parameter). 产品 就是返回值. 函数大致可以分为以下五个种类: 算术函数 ...
- 关于java中的数组
前言:最近刚刚看完了<Java编程思想>中关于数组的一章,所有关于Java数组的知识,应该算是了解的差不多了.在此再梳理一遍,以便以后遇到模糊的知识,方便查阅. Java中持有对象的方式, ...
- 静态链表C语言数据结构
静态链表就是将数组实现单链表: int Malloc_SLL(StaticLinkList space) { int i = space[0].cur;//取得第一个头节点的下标 if( space[ ...