一 having 过滤
1.1 having和where
select * from emp where id > 15;
解析过程;from > where 找到数据 > 分组(没有默认一个组)> select 打印 where是出结果之前
select * from emp having id > 15;
解析过程;from > where 找到数据(没有约束条件,就是整个表)) > 分组(没有默认一个组)> select 打印 > having where是出结果之后
上面2个输出是一样的,因为没有分组
select depart_id,count(id) from emp group by depart_id; 分组完后3个组,就是3个记录,就要通过聚合取值,通过分组字段取值
select depart_id,count(id) from emp group by depart_id where depart_id=1; 报错,这里改成having就好了
from emp group by depart_id 得出一张虚拟的表在内存里面,有2个字段depart_id,count(id)
在往后接就是针对这块虚拟的表,也就是where针对的是这个虚拟表,所以报错,因为where针对的是硬盘表
小结;where和having的区别
where是约束来自数据库的数据,是返回结果之前起作用的,从硬盘把数据where约束拿到内存,在分组之前
having是过滤声明,是结果已经到内存,在分组之后
1.2 聚合 把内容压成一个字段一条记录
select depart_id,count(id) from emp group by depart_id having name like 'ego%'; 报错,结果针对虚拟表,只有depart_id,count(id)2个字段
select depart_id,count(id) from emp group by depart_id having count(id) > 2; 在having里面的聚合最后就是字符串字段
from > where > groupby > 聚合 > select > having
select count(id) from emp where id > 15; 解析 from > where > groupby(默认是一组) > 聚合 > select
select count(id) from emp; 解析 from > where (没有约束条件,就是整个表) > groupby(默认是一组) > 聚合 > select
select count(id) from emp having id > 15; 报错 解析 因为having是groupby后的结果,只有count(id)这个字段
select * from emp having avg(salary) > 10000; 报错 聚合把内容压成一个字段一条记录,这条记录的名字是avg(salary)
select avg(salary) from emp;
select max(salary) from emp having avg(salary) > 10000;
解析 from > where (没有约束条件,就是整个表) > groupby(默认是一组) > 聚合max > 聚合avg > select > having
也就是过滤针对的是select打印做出的限制,限制为真,就打印select,否则为空
select 10000 from emp having 20000 > 10000; 从表中拿到数据,然后where,groupby然后判断过滤后面的,如果为真,就输出select
**顺序
select max(salary) from emp where id > 2 group by depart_id having avg(salary) > 10000;
from emp > where id>2 到内存 > group by depart_id 只有depart_id字段(select只能靠聚合) > 算出结果max(salary) avg(salary) > having 满足 > select
depart_id max(id) avg(salary)
1 3 10
2 1 11
1.3 练习
1. 查询各岗位内包含的员工个数小于2的岗位名、岗位内包含员工名字、个数
select post,count(id),group_concat(name) from emp group by post having count(id) < 2;
3. 查询各岗位平均薪资大于10000的岗位名、平均工资
select post,avg(salary),group_concat(name) from emp group by post having avg(salary) > 10000;
select post 部门,avg(salary) 平均工资,group_concat(name) 职工 from emp group by post having avg(salary) > 10000;
3. 查询各岗位平均薪资大于10000且小于20000的岗位名、平均工资
select post,avg(salary),group_concat(name) from emp group by post having avg(salary) between 10000 and 20000);
二 order by 关键字 排序 默认升序,asc
2.1 select * from emp order by salary; 按照薪资排序,从小到大,升序
select * from emp order by salary asc; 升序
select * from emp order by salary desc; 降序
年龄从小到大,出现相同再按照薪资去排
select * from emp order by age,salary;
select * from emp order by age(asc,desc),salary(asc,desc);
2.2 优先级
在having后面执行的,对打印出来的限制,也就是在select之前
2.3 练习
1. 查询所有员工信息,先按照age升序排序,如果age相同则按照hire_date降序排序
select * from employee ORDER BY age asc,hire_date desc;
2. 查询各岗位平均薪资大于10000的岗位名、平均工资,结果按平均薪资升序排列
select post 岗位名,avg(salary) 平均工资 from emp group by post having avg(salary) > 10000 order by 平均工资 asc;
3. 查询各岗位平均薪资大于10000的岗位名、平均工资,结果按平均薪资降序排列
select post 岗位名,avg(salary) 平均工资 from emp group by post having avg(salary) > 10000 order by 平均工资 desc;
** select name from emp where id > 5 having id > 16;
** select 后面决定了有什么字段,having,等限制打印的一定要限制后面的字段,不然就报错,因为没有这个字段
三 limit 限制查询记录数
3.1 select * from emp limit 3; 打印升序(默认就是升序)前三条
select * from emp order by id desc limit 3;打印后三条
3.2 分页查询,从哪开始,往后取几条
select * from emp limit 0,3; 0代表第一条,3代表查询几条
select * from emp limit 3,3; 3代表第四条
select * from emp limit 6,3;
四 使用正则表达式查询
4.1 like 模糊查询,% 匹配多个 _ 匹配一个
select * from emp where name like 'eg%';
select * from emp where name like '_g%';
4.2 正则 regexp 支持正则匹配
select * from emp where name regexp '^eg';
select * from emp where name regexp 'eg';
五 distinct 去重
select distinct sex from emp;
六 数学运算符
select name,salary from emp; 每个人名字,薪资
select name,salary*12 年薪 from emp;
#创建表
create table employee(
id int not null unique auto_increment,
name varchar(20) not null,
sex enum('male','female') not null default 'male', #大部分是男的
age int(3) unsigned not null default 28,
hire_date date not null,
post varchar(50),
post_comment varchar(100),
salary double(15,2),
office int, #一个部门一个屋子
depart_id int
);
#插入记录
#三个部门:教学,销售,运营
insert into employee(name,sex,age,hire_date,post,salary,office,depart_id) values
('egon','male',18,'','老男孩驻沙河办事处外交大使',7300.33,401,1), #以下是教学部
('alex','male',78,'','teacher',1000000.31,401,1),
('wupeiqi','male',81,'','teacher',8300,401,1),
('yuanhao','male',73,'','teacher',3500,401,1),
('liwenzhou','male',28,'','teacher',2100,401,1),
('jingliyang','female',18,'','teacher',9000,401,1),
('jinxin','male',18,'','teacher',30000,401,1),
('成龙','male',48,'','teacher',10000,401,1),
('歪歪','female',48,'','sale',3000.13,402,2),#以下是销售部门
('丫丫','female',38,'','sale',2000.35,402,2),
('丁丁','female',18,'','sale',1000.37,402,2),
('星星','female',18,'','sale',3000.29,402,2),
('格格','female',28,'','sale',4000.33,402,2),
('张野','male',28,'','operation',10000.13,403,3), #以下是运营部门
('程咬金','male',18,'','operation',20000,403,3),
('程咬银','female',18,'','operation',19000,403,3),
('程咬铜','male',18,'','operation',18000,403,3),
('程咬铁','female',18,'','operation',17000,403,3)
;
多表查询
整体是一个数据,方便管理,节省空间拆成不同的表
company.employee
company.department
==============================被关联表
创建 被关联表
create table department(
id int,
name varchar(20)
)charset utf8;
插入数据
insert into department values
(200,'技术'),
(201,'人力资源'),
(202,'销售'),
(203,'运营');
===============================关联表
创建 关联表
create table employee(
id int primary key auto_increment,
name varchar(20),
sex enum('male','female') not null default 'male',
age int,
dep_id int
)charset utf8;
插入数据
insert into employee(name,sex,age,dep_id) values
('egon','male',18,200),
('alex','female',48,201),
('wupeiqi','male',38,201),
('yuanhao','female',28,202),
('liwenzhou','male',18,200),
('jingliyang','female',18,204)
;
** 暂时不加入外键,如果假如外键,关联表在拆入数据的时候,插入204,上面的表没有下面的表对应关系
上有下没有,下有上没有,就会报错
上表有203下面没有,下面204上面没有
** 虽然没有外键,咱们假设department id 和 employee dep_id 关联