MySQL单表查询
未分组查询 | 说明 | 分组查询 | 说明 |
---|---|---|---|
select | 标识查询后的展示结果字段 | group by | 对表中的数据先根据字段值的不同进行不同的组,来查询组中共同拥有的数据和统计的语句 注意:分组查询后的结果必须是分组的依据或聚合函数 |
from | having | having的使用是必须紧跟group by ,使用用来对分组后查询的结果进行二次筛选。 | |
where | 放置的是查询的条件 | ||
order by | 主要是用来实现排序的 | ||
limit | 对查询后的结果进行分页展示 |
以下示例使用teacher进行测试:
create table teacher(
id int,
name varchar(50),
sex char(1),
address varchar(100),
age int,
salary decimal(10,2),
birthday date
);
-- 插入数据
insert teacher(id,name,sex,address,age,salary,birthday)
values
(1,'孙老师','女','河南省郑州市高新区',25,'7000.00','1999-02-12'),
(2,'赵阳老师','男','河南省郑州市金水区',25,'6800.00','2020-03-13'),
(3,'李如花老师','女','河南省郑州市二七区',25,'7698.30','2021-02-02'),
(4,'王富贵老师','男','河南省郑州市金水区',25,'7200.00','2022-02-22'),
(5,'李大钊老师','女','河南省郑州市中原区',25,'6590.60','2008-10-21'),
(6,'赵舒文老师','女','河南省郑州市金水区',25,'8980.00','2000-11-12'),
(7,'秦阳老师','男','河南省郑州市二七区',25,'9800.00','2009-05-12'),
(8,'孙美胜老师','女','河南省郑州市高新区',25,'102000.00','2012-02-02'),
(9,'赵文博老师','女','河南省郑州市惠济区',25,'12300.00','2015-08-14'),
(10,'王老师','男','河南省郑州市高新区',25,'6809.00','2018-03-09'),
(11,'孙老师','女','河南省郑州市金水区',25,'5800.00','2019-06-16'),
(12,'李老师','男','河南省郑州市中原区',25,'7500.00','2011-04-19'),
(13,'孙老师','女','河南省郑州市高新区',25,'7600.00','2013-05-30');
select
标识查询后的展示结果字段
*:代表的是表中的所有的字段信息
字段名 as 别名:查询表中部分字段信息并取别名,as可以省略
distinct:去除重复行
四则运算:查询后的结果可以进行四则运算
示例:
select * from teacher
-- 查询老师的姓名、性别和住址
select name,address,sex from teacher
-- 查询老师的name取名为姓名,address为地址
select name as 姓名,address 地址 from teacher
-- 查询老师的性别,去重
select distinct sex from teacher
-- 查询老师的性别,地址,去重
select distinct sex,address from teacher
-- 查询老师的姓名和三年后的年龄
select name,age+3 from teacher
-- 查询老师的姓名和月薪
select name,salary from teacher
-- 查询老师的姓名和年薪,并取别名
select name 姓名,salary*12 年薪 from teacher
-- 查询姓名和月薪,合并两列
select concat(name,salary) from teacher
where
where放置的是查询的条件。
1,比较运算符 > >= < <= = != <>
2,连接符 and or
3,范围运算符 between … and … not between … and …
in(…) not in(…)
4,模糊
like …
%: 零个或多个字符
_: 一个字符
5,是否为null
is null
is not null
-- 查询工资高于7000的员工的姓名及工资
select name,salary from teacher where salary>7000
-- 查询年薪高于100000的员工的姓名及年薪
select name,salary*12 from teacher where salary*12>100000
order by
主要是用来实现排序的
排序规则:asc升序(不写的话,默认升序) desc 降序
-- 查询老师信息,根据年龄降序排序,如果年龄相同则根据id升序排序
select * from teacher order by age desc,id asc
-- 查询所有的男老师,根据年龄降序排序,如果年龄相同,则根据工资降序排序
select * from teacher where sex='男' order by age desc,salary desc
先写select 再写from 再写where 再写order by顺序不能打乱
order by如果满足第一个条件第二个就不会执行
limit
对查询后的结果进行分页展示
格式:
limit count; 展示count条数据,默认从0下标开始;
limit index, count; 从index下标开始,向后查询count条数据,展示count条数据
-- 根据编号升序排序,展示前三条数据
select * from teacher order by id asc limit 3;
select * from teacher order by id asc limit 0,3;
select * from teacher order by id asc limit 3,3;
-- 查询所有的男老师,根据编号升序排序,只显示前2条数据
select * from teacher where sex='男' order by id asc limit 2
-- 查询年龄最大的两位
select * from teacher order by age desc limit 2;
group by
分组查询
对表中的数据先根据字段值的不同进行不同的组,来查询组中的语句
注意:分组查询后的结果必须是分组的依据或聚合函数
关键字:各个(这时候就需要使用group by)
例子(帮助理解group by的作用):
-- 会根据age的值分为多个不同的组(也就是行)
select age,count(*) from teacher group by age;
注:group by之后也可以进行order by的排序和limit的分页展示
问:使用group by查询后的数据有多少条?
答:会根据分组依据值的不同分为不同的组
-- 创建一张职工表表并插入数据
create table emp2(
empno int primary key auto_increment,
ename varchar(4),
job varchar(4),
mgr varchar(4),hiredate date,
sal decimal(7,2),
Comm decimal(7,2),
deptno int
);
insert into emp2(empno,ename,job,mgr,hiredate,sal,Comm,deptno)values
(1001,'甘宁','文员','1013','2020-12-17',8000.00,null,20),
(null,'黛琦丝','销售员','1006','2001-02-20',16000.00,3000.00,30),
(null,'殷天正','销售员','1006','2001-02-22',12500.00,5000.00,30),
(null,'刘备','经理','1009','2001-04-02',29750.00,null,20),
(null,'谢','销售员','1006','2001-09-28',12500.00,14000.00, 30),
(null,'关羽','经理','1009','2001-05-01', 28500.00, null, 30),
(null,'张飞','经理','1009',' 2001-09-01', 24500.00, null, 10),
(null,'诸葛亮','分析师','1004',' 2007-04-19', 30000.00, null, 20);
select * from emp2
-- 查询不同的职位个有多少人,最高工资,最低工资
select job,count(1),max(sal),min(sal)
from emp2
group by job
-- 查询各个不同的部门各有多少人,总工资多少?
select deptno,count(1),sum(sal)
from emp2
group by deptno
-- 查询在2015年前入职的各个部门的人数
select deptno,count(1)
from emp2
where hiredate<'2015-1-1'
group by deptno
-- 查询各个不同的部门各有多少人,结果按照人数降序排列
select deptno,count(1) as co
from emp2
group by deptno
order by co desc
-- 查询各个不同的部门各有多少人,结果按照人数降序排列并只展示一条
select deptno,count(1) as co
from emp2
group by deptno
order by co desc limit 1
聚合函数
函数 | 作用 |
---|---|
count(字段) | 求数据个数 |
sum(字段) | 求和 |
avg(字段) | 求平均值 |
max(字段) | 求最大值 |
min(字段) | 求最小值 |
特别需要说明的是
count的三种用法:
count的三种用法 | 特点 | 说明 |
---|---|---|
count(*) | 求表中的数据总条数,正确率高,效率较低 | 查询时会根据表中所有字段分别查询有多少条然后进行整合 即根据所有的字段判断总条数 |
count(字段) | 求表中的该字段的值非空时的总条数,正确率较低,效率较高 | 根据某个字段来统计,如果该字段中有空值,则会出错 |
count(1)【推荐使用】 | 求表中的数据总条数,正确率较高,效率较高 | 根据主键编号来统计 |
having
having的使用必须紧跟group by,必须前面是分组
(可以理解为是try catch后的finally,需要了就用,不需要就不用)
用来对分组后查询的结果进行二次筛选
-- 查询工资高于9000的各个不同职位的员工人数,展示人数高于2人以上,结果以人数降序排列
select job,count(1) co from emp2 where sal>9000 group by job having co>=2 order by co desc limit 1
注:关键字的顺序不能写错
select --> from --> where --> group by -->having --> order by --> limit
where 和having的区别:
where | having |
---|---|
在分组查询前进行筛选 | 在分组查询后查询的结果中进行二次筛选 |
可以单独使用 | 必须和group by组合使用 |