单表查询:
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);
书写顺序 select * from emp where id>3 and id <6; 执行顺序: 1.先找到emp这张表(from) # 先确定是哪张表 2.去找id在3-6之间的所有字段(where) # 再确定是否有过滤条件 3.*是在这之间所有字段我都要(select) # 最后确定要过滤出来的数据的哪些字段
1.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; #between在什么之间 2.查询薪资是20000或者18000或者17000的数据 select * from emp where salary=20000 or salary=17000 or salary=18000; select id,name emp where salary in (18000,20000,17000); 3.查询员工姓名包含0字母的员工姓名和薪资 select name,salary from emp where name like '%0%'; # like模糊匹配 4.查询员工姓名是由四个字符组成的员工姓名和薪资 select name,salary from emp where name like '____';_单个任意字符 select name,salary from emp where char_length(name)=4; 5.查询薪资不是20000,18000,17000的数据 select id,name emp where salary not in (18000,20000,17000); 6.查询岗位描述为空的员工名与岗位名 判断null只能用is python也是一样 select name,post from emp where post_comment is null;
2.group by分组
1.按部门分组 select * from emp group by post; # 分组之后只能获取到分组依据的字段值,不能直接获取组内单个数据的信息 #设置严格模式 set global sql_mode='strict_trans_tables,only_full_group_by'; 设置之后只能查看post字段信息,其余看不了
3.聚合函数
#获取每个部门的最高工资 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,count(id) from emp group by post; select post,count(id) from emp group by post; # group_concat(name) #获取到组里面所有你需要数据如,name,salary # group_concat(name,'DSB') #支持拼接,name后面都加上DSB #查询分组之后的部门名称和每个部门下所有的学生姓名 select post,group_concat(name) from emp group by post; #as语法 # 给字段起别名 select post as '部门',group_concat(name,'DSB') as '员工姓名' from emp group by post; concat() # 用于不分组的情况下字符串的拼接 select id ,concat('NAME',name) from emp where id <5; select from where group by 执行顺序 from > where > group by >select select max(salary) from emp; #先找到emp表,再看有没有条件,没有分组,整体是一组 #查询每个人年薪 select name,salary*12 as '年薪' from emp; 1.查询岗位名以及岗位包含的所有员工名字 select post,group_concat(name),from emp group by post; 2.查询岗位名以及各岗位内包含的员工数 select post,count(id) from emp group by post; 3.查询公司内男员工和女员工的个数 select gender,count(id) from emp group by gender; 4.查询岗位名以及各岗位的平均薪资 select post,avg(salary) from emp group by post; 5.查询岗位名以及岗位的最高薪资 select post,max(salary) from emp group by post; 6.查询岗位名以及各岗位的最低薪资 select post,min(salary) from emp group by post; 7.查询男员工与男员工的平均薪资,女员工与女员工的平均薪资 select gender,avg(salary) from emp group by gender; 8.统计各部门年龄在30岁以上的员工平均工资 select * from emp where age>30 ; select post,avg(salary) from select * from emp where age>30 group by post;
4.having:
过滤(保留)
用法和where一样,having用在group by之后 也就意味着where不能用聚合函数,但是having可以 执行顺序 from > where >group by > having >select 1.统计各部门年龄在30岁以上的员工平均薪资,并且保留平均工资大于10000的部门 select * from emp where age>30; select post,avg(salary) from emp where age>30 group by post having avg(salary)>10000;
5.distinct:
去重
select post from emp; select distinct post from emp; 执行顺序 from > where >group by > having >distinct >select
6.order by
排序
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 desc,salary asc;年龄降序,薪资升序 #统计各部门年龄在10岁以上的员工平均工资,并且保留平均工资大于1000的部门,然后对平均工资进行排序 select * from emp where age>10; select post ,avg(salary) from emp where age>10 group by post having avg(salary)>1000 order by avg(salary);
limit
限制查询条数
select * from emp limit 5; # 所有数据拿五条 select * from emp limit 5,5;# 从第五条以后开始拿5条 两个参数,一个是起始位置,后面是往后显示个数 #查询工资最高的人详细信息 select max(salary) from emp; #查询最高的工资 select * from emp order by salary desc limit 1; #从最高工资排序,显示第一行所有信息
%_
正则(regexp)
贪婪匹配与非贪婪匹配:.* .*? select * from emp where name regexp '^j.*(n|y)$'; 所有语法顺序: select distinct * from '表名' where '限制条件' group by '分组依据' having '过滤条件' order by limit
多表查询:
create table dep(id int, name varchar(20)); create table emp1(id int primary key auto_increment, name varchar(20), sex enum('male','female') not null default 'male', age int, dep_id int); 笛卡尔积 select * from emp,dep where emp.dep_id = dep.id; # 查询员工及所在部门的信息 select * from emp,dep where emp.dep_id = dep.id; # 查询部门为技术部的员工及部门信息 select * from emp,dep where emp.dep_id = dep.id and dep.name = '技术'; # 将两张表关联到一起的操作,有专门对应的方法 # 1、内连接:只取两张表有对应关系的记录 inner join 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、左连接: 在内连接的基础上保留左表没有对应关系的记录 left join select * from emp left join dep on emp.dep_id = dep.id; # 3、右连接: 在内连接的基础上保留右表没有对应关系的记录 right join select * from emp right join dep on emp.dep_id = dep.id; # 4、全连接:在内连接的基础上保留左、右面表没有对应关系的的记录 union select * from emp left join dep on emp.dep_id = dep.id union select * from emp right join dep on emp.dep_id = dep.id;
子查询:
就是将一个查询语句的结果用括号括起来当作另外一个查询语句的条件去用 #1.查询部门是技术或者人力资源 select id from dep where name in ('技术','人力资源'); select * from emp where dep_id in (select id from dep where name in ('技术','人力资源')); select * from emp where dep_id in (200,201); # 等价同上 # 2.每个部门最新入职的员工 思路:先查每个部门最新入职的员工,再按部门对应上联表查询 select t1.id,t1.name,t1.hire_date,t1.post,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; 记住一个规律,表的查询结果可以作为其他表的查询条件,也可以通过其别名的方式把它作为一张虚拟表去跟其他表做关联查询 select * from emp inner join dep on emp.dep_id = dep.id;