单表查询 多表查询 子查询
单表查询
where group by having distinct order by limit
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
create table emp( 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 emp(name,sex,age,hire_date,post,salary,office,depart_id) values ('jason','male',18,'20170301','张江第一帅形象代言',7300.33,401,1), #以下是教学部 ('egon','male',78,'20150302','teacher',1000000.31,401,1), ('kevin','male',81,'20130305','teacher',8300,401,1), ('tank','male',73,'20140701','teacher',3500,401,1), ('owen','male',28,'20121101','teacher',2100,401,1), ('jerry','female',18,'20110211','teacher',9000,401,1), ('nick','male',18,'19000301','teacher',30000,401,1), ('sean','male',48,'20101111','teacher',10000,401,1), ('歪歪','female',48,'20150311','sale',3000.13,402,2),#以下是销售部门 ('丫丫','female',38,'20101101','sale',2000.35,402,2), ('丁丁','female',18,'20110312','sale',1000.37,402,2), ('星星','female',18,'20160513','sale',3000.29,402,2), ('格格','female',28,'20170127','sale',4000.33,402,2), ('张野','male',28,'20160311','operation',10000.13,403,3), #以下是运营部门 ('程咬金','male',18,'19970312','operation',20000,403,3), ('程咬银','female',18,'20130311','operation',19000,403,3), ('程咬铜','male',18,'20150411','operation',18000,403,3), ('程咬铁','female',18,'20140512','operation',17000,403,3) ;
1 语法执行顺序
书写顺序
select id,name from emp where id > 3 and id <6;
执行顺序
from #先确定那张表
where #再确定是否有过滤条件
select # 最后确定要过滤出来的数据的那些字段
select id,name from emp where id >3;
2 where 约束条件
1 查询id大于等于3 小于等于6的数据
select * from emp where id >=3 and id <=6;
select id ,name from emp where id between 3 and 6;
2 查询薪资是20000 或者18000或者17000的数据
select * from emp where salary=20000 or salary =18000 or salary=17000;
select id,name from emp where salary in(20000,18000,17000);
3 查询员工姓名中包含o字母的员工姓名和薪资
select name,salary from emp where name like '%o%';
4 查询员工姓名是由四个字符组成的员工姓名与其薪资
select name from emp where name like '____';
select name from emp where char_length(name)=4;
5 查询id小于3或者大于6的数据
select * from emp where id not between 3 and 6;
6 查询薪资不在20000,18000,17000范围的数据
select id ,name from emp where salary not in (20000,18000,17000);
7 查询岗位描述为空的员工名与岗位名 判断空不能用等号只能用is
select name ,post from emp where post_comment=NULL #报错
select name,post from emp where post_comment is NULL;
3 group by 分组
max() 分组内最大值
avg() 分组内平均值
sum() 分组内总和
count() 分组内出现的个数
group_concat(name) 统计分组下的所有学生姓名
as 可以给字段和表起别名
concat() 未分组字符串拼接
1 按部门分组 select * from emp group by host ; #分组取出的是每个组的第一条是数据 select id,name,sex from emp group by post; #验证一下#只取一个 #分组之后只能获取到分组依据的字段值,不能直接获取到组内单个数据的信息 设置严格模式 set global sql_mode='strict_trans_tables,only_full_group_by'; 重新连接客户端 select * from emp group by post; #报错 select id,name,sex from emp group by post ; #报错 select post from emp group by post; #获取部门信息
#强调:只要分组了,就不能够再‘直接’查到单个数据信息了,只能获取到组名 2 获取每个部门的最高工资 #以组为单位统计组内数据 >> 聚合查询(聚集到一起合成为一个结果) select post ,max(salary) from emp group by post;
# 每个部门的最低工资 select post,min(salary) from emp group by post;
#每个部门的平均工资 select post,avg(salary) from emp group by post;
# 每个部门的工资总和 select post,sum(salary) from emp group by post;
# 每个部门的人数 select post,count(id) from emp group by post; select post ,count(salary) from emp group by post;
3 查询分组之后的部门名称和每个部门下所有的学生姓名 group_concat() 分组之后用 不仅可以用来显示除分组外字段 还有拼接字符串的作用 select post,group_concat(name) from emp group by post;
select post,group_concat(name,'DSB') from emp group by post;
as语法 可以给字段起别名也可以给表起别名 as可以省略不写 建议写可读性好 select post as '部门' ,group_concat(name,'DSB') as '员工姓名' from emp group by post;
select post '部门' ,group_concat(name,'DSB') '员工姓名' from emp group by post;
select emp.id ,emp.name from emp as t1; #报错 因为表名已经被改成了t1 select t1.id ,t1.name from emp as t1; concat() 不分组时用 拼接字符串达到更好的显示效果 select id,concat('NAME:',name) from emp where id <5;
查询四则运算 查看每个人的年薪 select name,salary*12 as annual_salary from emp; select name,salary*12 annual_salary from emp; #as 省略
书写顺序 select 聚合函数 from where group by
执行顺序
from
where
group by
select
select max(salary) from emp; #emp表下薪资最大的
关键字where group by同时出现的情况下,group by必须在where之后
where先对整张表进行一次筛选,group by 在对筛选过后的表进行分组
验证where是在group by 之前而不是之后 利用聚合函数 因为聚合函数只能在分组之后才能使用
select id, name ,age from emp where max(salary) >3000; #报错
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
4 having 分组后过滤
用法跟where一毛一样 ,having用在group by之后
也就意味着where 不能用聚合函数,但是having可以
1、统计各部门年龄在30岁以上的员工平均工资,并且保留平均工资大于10000的部门
select post,avg(salary) from emp where age>30 group by post having avg(salary)>10000;
![](https://img2018.cnblogs.com/blog/1634607/201905/1634607-20190515204936663-1437571008.png)
5 distinct 去重
#对有重复的展示数据进行去重操作
select post from emp; #未去重 select distinct post from emp;
![](https://img2018.cnblogs.com/blog/1634607/201905/1634607-20190515205426658-2009353775.png)
执行顺序
from
where
group by
having
distinct
select
6 order by 排序 asc升序 默认 desc 降序
select * from emp order by salary asc ; #默认升序排 select * from emp order by salary desc ; #降序排 select * from emp order by age desc ; #降序排 #先按照age降序排,在年龄相同的情况下再按照薪资升序排 select *from emp order by age desc ,salary asc; # 统计各部门年龄在10岁以上的员工平均工资,并且保留平均工资大于1000的部门,然后对平均工资进行排序 select post, avg(salary) from emp where age>10 group by post having avg(salary) >1000
order by ave(salary);
7 limit
#限制展示条数 select * from emp limit 3;#查询工资最高的人的详细信息 select * from emp order by salary desc limit 1;
#分业显示 select * from emp limit 0,5; #第一个参数表示起始位置,第二个参数表示的从第一个参数的位置开始往后获取的数据个数,不是索引位置 select * from emp limit 5,5;
8 正则 用regexp声明
select * from emp where name regexp'^j.*(n|y)$';
![](https://img2018.cnblogs.com/blog/1634607/201905/1634607-20190515210851536-1537226238.png)
多表查询
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#建表 create table dep( id int, name varchar(20) ); create table emp( id int primary key auto_increment, name varchar(20), sex enum('male','female') not null default 'male', age int, dep_id int ); #插入数据 insert into dep values (200,'技术'), (201,'人力资源'), (202,'销售'), (203,'运营'); insert into emp(name,sex,age,dep_id) values ('jason','male',18,200), ('egon','female',48,201), ('kevin','male',38,201), ('nick','female',28,202), ('owen','male',18,200), ('jerry','female',18,204) ;
我们当初分表(不分表1 组织结构不清晰 2 浪费硬盘空间 3 扩展性极差),就是为了方便管理,在硬盘上确实是多张表,但是到了内存中我们应该把他们在拼成一张表进行查询才合理
表查询 select * from emp ,dep; #左表一条记录与右表所有记录都对应一遍>>>笛卡尔积 #将所有的数据都对应了一遍,虽然不合理但是其中有合理的数据,现在我们需要做的就是找出合理的数据 #查询员工所在部门信息 select * from emp ,dep where emp.dep_id =dep.id;#查询部门为技术部的员工及部门信息 select * from emp,dep where emp.dep_id=dep.id and dep.name='技术';
将两张表关联到一起的操作,有专门对应的方法 1 内连接 :只取两张表有对应关系的记录 select * from emp inner join dep on emp.dep_id=dep.id; select * from emp inner join dep on emp.dep_id =dep.id where dep.name='技术'; 2 左连接:在内连接的基础上保留左表没有对应关系的记录 select * from emp left join dep on emp.dep_id =dep.id; 3 右连接:在内连接的基础上保留右表没有对应关系的记录 select * from emp right join dep on emp.dep_id =dep.id; 4 全连接:在内连接的基础上保留左、右面表没有对应关系的记录 select * from emp left join dep on emp.dep_id=dep.id union select * from emp right jon dep on emp.dep_id=dep.id;
子查询
就是将一个查询语句的结果用括号括起来 当做另外一个查询语句的条件去用1查询部门是技术或者人力资源的员工信息
‘先获取技术部和人力资源部的id号 再去员工表里面根据前面的id筛选出符合要求的员工信息’ select * from emp where dep_id in(select id from dep where name='技术' or name='人力资源'); 2 每个部门最新入职的员工 思路:先查询每个部门最新入职的员工,再按部门对应上联表查询 #伪代码 select id, name from emp as t1
t2.* t2的表所有信息 select t2.* ==>select *(默认当前表)
from emp as t1(emp所有信息作为t1表) select t1.id,t1.name,t1.hire_date,t1.post,t2.* from emp as t1
inner join #匹配两张表有共有的(post)
(select post,max(hire_date) as max_date from emp group by post) as t2
on t1.post=t2.post
where t1.hire_date=t2.max_date;
二种:
select * from emp as t1
inner join
(select post,max(hire_date)as max_date from emp group by post) as t2
on t1.post=t2.post
where t1.hire_date=t2.max_date;
三:
select t1.id ,t1.name,t1.sex,t2.* from emp as t1
inner join
(select post,max(hire_date)as max_date from emp group by post) as t2
on t1.post=t2.post
where t1.hire_date=t2.max_date;
''' 虚拟表!! 记住一个规律,标的查询结果可以作为其他表查询的条件, 也可以通过起别名的方法把它作为一张虚拟表去更其他表做关联查询!! '''