python开发mysql:单表查询&多表查询
一 单表查询,以下是表内容
一 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;
二 多表查询,一下是表内容
多表查询
一 简单查询 多张表 提取相同的 假设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 关联
python开发mysql:单表查询&多表查询的更多相关文章
- python 3 mysql 单表查询
python 3 mysql 单表查询 1.准备表 company.employee 员工id id int 姓名 emp_name varchar 性别 sex enum 年龄 age int 入职 ...
- python开发mysql:表关系&单表简单查询
一 一对多,多对一 1.1 建立多对一 ,一对多的关系需要注意 先建立被关联的表,被关联的字段必须保证时唯一的 在创建关联的表,关联的字段一定是可以重复的 1.2 示例: 出版社 多对一,多个老师可能 ...
- python开发mysql:mysql安装(windows)&密码找回&存储引擎简介&库表的增删改查
一,mysql安装 下载地址 https://dev.mysql.com/downloads/file/?id=471342 解压后,将目录C:\mysql-5.7.19-winx64\bin添加到计 ...
- Python操作Mysql实例代码教程在线版(查询手册)
本文介绍了Python操作MYSQL.执行SQL语句.获取结果集.遍历结果集.取得某个字段.获取表字段名.将图片插入数据库.执行事务等各种代码实例和详细介绍,代码居多,是一桌丰盛唯美的代码大餐 实 ...
- Python操作Mysql实例代码教程在线版(查询手册)_python
实例1.取得MYSQL的版本 在windows环境下安装mysql模块用于python开发 MySQL-python Windows下EXE安装文件下载 复制代码 代码如下: # -*- coding ...
- python开发mysql:索引
一,索引管理 索引分类: 普通索引INDEX:加速查找 唯一索引: -主键索引PRIMARY KEY:加速查找+约束(不为空.不能重复) -唯一索引UNIQUE:加速查找+约束(不能重复) 联合索引: ...
- Python与Mysql 数据库的连接,以及查询。
python与mysql数据库的连接: pymysql是python中对数据库的连接模块:因此应当首先安装pymysql数据库模块. 执行pip install pymysql 命令. 然后在pyth ...
- python开发mysql:mysql数据类型&约束条件
一 整形 只有Int类型跟存储没有关系,显示的是宽度,其他类型都是限制 整形类型:[(m)][unsigned][zerofill] 作用:存储年龄,等级,id,各种号码 m,代表显示宽度 默认11 ...
- python 统计MySQL大于100万的表
一.需求分析 线上的MySQL服务器,最近有很多慢查询.需要统计出行数大于100万的表,进行统一优化. 需要筛选出符合条件的表,统计到excel中,格式如下: 库名 表名 行数 db1 users 1 ...
随机推荐
- php:Mcrypt响应慢的原因解决备注
作者: Laruence 本文地址: http://www.laruence.com/2012/09/24/2810.html 转载请注明出处 上午的时候, 有同事来找我说上周新上线的一个使用mcry ...
- 快速掌握Android 虚拟机(AVD)方法
摘自 http://jingyan.baidu.com/article/15622f24707710fdfdbea553.html 参阅:http://jingyan.baidu.com/articl ...
- Python 面向对象-------补充
Python 面向对象 Python从设计之初就已经是一门面向对象的语言,正因为如此,在Python中创建一个类和对象是很容易的.本章节我们将详细介绍Python的面向对象编程. 如果你以前没有接触过 ...
- ubuntu下搭建hadoop平台
终于把单击模式跟伪分布式模式搭建起来了,记录于此. 1.SSH无密码验证配置 因为伪分布模式下DataNode和NameNode均是本身,所以必须配置SSH localhost的无密码验证. 第一步, ...
- LeetCode OJ:Find Peak Element(寻找峰值元素)
A peak element is an element that is greater than its neighbors. Given an input array where num[i] ≠ ...
- 基于Python和Tornado的WEB Terminal
https://github.com/liftoff/GateOne 亮点有以下: ↪ Clientless ↪ Multi-User and Multi-Session ↪ Multi-Auth a ...
- Spring核心AOP(面向切面编程)总结
(尊重劳动成果,转载请注明出处:http://blog.csdn.net/qq_25827845/article/details/75208354冷血之心的博客) 1.AOP概念: 面向切面编程,指扩 ...
- warning MSB8004: Output Directory does not end with a trailing slash.
当在VC里编译时,发现这个警告,就是说设置的目录参数不是以反斜杠结束的目录名称,如下: 1>C:\Program Files (x86)\MSBuild\Microsoft.Cpp\v4.0\V ...
- Leetcode 1005. Maximize Sum Of Array After K Negations
class Solution(object): def largestSumAfterKNegations(self, A, K): """ :type A: List[ ...
- Games Delphi developers play
Original link: Games Delphi developers play Delphi game developers are stupid people having too ma ...