一 单表查询,以下是表内容

python开发mysql:单表查询&多表查询-LMLPHP

 一 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;

二 多表查询,一下是表内容

python开发mysql:单表查询&amp;多表查询-LMLPHP

 多表查询
一 简单查询 多张表 提取相同的 假设department id 和 employee dep_id 关联
1.1 select * from department,employee; 结果显示一张表上,是左右字段一次排开拼接这种
遍历第一张表,循环第二张,配对一遍,显示出来,拼接方式叫做 笛卡尔积
select * from department,employee where department.id=employee.dep_id;
select * from employee,department where department.id=employee.dep_id; 提取2表相同的部分 二 连表操作
2.1 内链接 inner join department on
按照on条件只取2表的相同部分,链接成一张虚拟的表
select * from employee inner join department on department.id=employee.dep_id; 提取2表相同的部分 2.2 左链接 left join department on
按照on条件取2表的相同部分基础上,保留左表有,右边没有的记录
select * from employee left join department on department.id=employee.dep_id; 2.3 右链接 right join department on
按照on条件取2表的相同部分基础上,保留右表有,左边没有的记录
select * from employee right join department on department.id=employee.dep_id; 2.4 全链接 full join 在mysql里面没有这个功能,只是个概念,还要考左,右链接拼凑出来
有对应关系,和没对应关系的都取出来
select * from employee left join department on department.id=employee.dep_id
union 联合的意思,将2表联合,去掉重复的部分
select * from employee right join department on department.id=employee.dep_id; ** 优先级
from > join on 后面的条件联合 > where > gourp by > 聚合 > having > order by > limit > select 三 子查询
3.1 带in关键字的子查询
select id from department where name in ('技术','销售'); 查看2个部门的d
select * from employee where dep_id in (200.202); 查询部门id对应的员工
问题;怎么知道员工的id,应该是从部门表得到id
select * from employee where dep_id in (select id from department where name in ('技术','销售')); 3.2 练习
1. 查询平均年龄在25岁以上的部门名
select name from department where id in (select dep_id from employee group by dep_id having avg(age) > 25);
2. 查看技术部员工姓名
select name from employee where dep_id in (select id from department where name='技术');
3. 查看人数小于2的部门,不准确,因为还有空的存在,看 4 5 题
select name from department where id in (select dep_id from employee group by dep_id having count(dep_id) < 2);
4. 提取空部门,就是没有人的部门
select distinct dep_id from employee; 去重,有人的部门
select name from department where id not in (select distinct dep_id from employee); 非有人的部门,空部门
5. 查看人数小于2的部门,但是有空的情况
select name from department where id in (select dep_id from employee group by dep_id having count(dep_id) < 2)
union
select name from department where id not in (select distinct dep_id from employee); 没有人的部门 或者:
select name from department where id in
(
select dep_id from employee group by dep_id having count(id) < 2
union
select id from department where id not in (select distinct dep_id from employee)
); 3.3 exists存在,判断
select * from employee where exists (select id from department where name='技术');
select id from department where name='技术' 存在 就打印前面的select,不存在就返回空 ** 子查询,括号内的查询都要select出2表相同的字段,这样才可以,语法:查询 where 相同字段 in 子查询

三 总结&表创建过程

 总结;
查询语法
SELECT 字段1,字段2... FROM 表名
JOIN ON
WHERE 条件
GROUP BY field
HAVING 筛选
ORDER BY field
LIMIT 限制条数 关键字的执行优先级
from
join on
where
---------------
group by
---------------
聚合
having
select
---------------
限制打印
distinct
order by
limit

单表创建

 #创建表
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 关联
04-30 10:33