语法执行顺序:
书写顺序
select distinct * from '表名' where '限制条件' group by '分组依据' having '过滤条件' order by limit;
执行顺序
from
where
group by
having
order by
limit
distinct
select
一、单表查询
先建立表格 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、where:查询
①查询id大于等于3小于等于6的数据 select * from emp where id between 3 and 6; ②查询薪资是20000或者18000或者17000的数据 select * from emp where salary in (20000,18000,17000); ③查询员工姓名中包含o字母的员工姓名和薪资 select name,salary from emp where name like '%o%'; ④查询id小于3或者大于6的数据 select * from emp where id not between 3 and 6; ⑤查询薪资不在20000,18000,17000范围的数据 select * from emp where salary not in (20000,18000,17000); ⑥查询岗位描述为空的员工名与岗位名:针对null不能用等号,只能用is select name,post from emp where post_comment is null;
2、group by:分组
应用场景:每个部门的平均薪资,男女比例等
①按部门分组 select * from emp group by post; # 报错 强调:只要分组了,就不能够再“直接”查找到单个数据信息了,只能获取到组名 select post from emp group by post; ②获取每个部门的最高工资 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; ③查询分组之后的部门名称和每个部门下所有的学生姓名 group_concat(分组之后用)不仅可以用来显示除分组外字段还有拼接字符串的作用 select post,group_concat(name) from emp group by post; select post,group_concat(name,'sb') from emp group by post; ④补充concat(不分组时用)拼接字符串达到更好的显示效果 as语法使用 select name as 姓名,salary as 薪资 from emp; 补充as语法 即可以给字段起别名也可以给表起 select t1.id,t1.name from emp as t1; ⑤查询每个人的年薪 select name,salary*12 as annual_salary from emp; ⑥查询公司内男员工和女员工的个数 select sex,count(id) from emp group by sex; ⑦查询男员工与男员工的平均薪资,女员工与女员工的平均薪资 select sex,avg(salary) from emp group by sex; ⑧关键字where group by同时出现的情况下,group by必须在where之后 where先对整张表进行一次筛选,如何group by再对筛选过后的表进行分组 ⑨统计各部门年龄在30岁以上的员工平均工资 select post,avg(salary) from emp where age>30 group by post;
3、having:限制条件,用在group by后
having的语法格式与where一致,只不过having是在分组之后进行的过滤, 即where虽然不能用聚合函数,但是having可以!
统计各部门年龄在30岁以上的员工平均工资,并且保留平均工资大于10000的部门 select post,avg(salary) from emp where age>30 group by post having avg(salary)>10000 ;
4、distinct:去重
对有重复的展示数据进行去重操作
select distinct post from emp;
5、order by:排序
①工资升序 select * from emp order by salary asc; ②工资降序 select * from emp order by salary desc; ③升序,年龄一样,按薪资排 select * from emp order by age,salary asc; ④统计各部门年龄在10岁以上的员工平均工资,并且保留平均工资大于1000的部门,然后对平均工资进行排序 select post,avg(salary) from emp where age>10 group by post having avg(salary)>1000 order by avg(salary) asc;
6、limit:限制查询条件
①限制展示条数 select * from emp limit 5; ②分页显示 如果写了俩参数,第一个参数表示起始位置,第二个参数表示的从第一个参数的位置开始往后获取的数据个数 select * from emp limit 5,5; (起始位置,显示个数) ③查询工资最高的人的详细信息 select * from emp order by salary desc limit 1;
7、regexp:正则
贪婪匹配与非贪婪匹配:.* .*?
查询以j开头,以n或y结尾,尽可能多的匹配 select * from emp where name regexp '^j.*(n|y)$';
二、多表查询
先创建表格: create table dep( id int, name varchar(16) ); create table emp_1( id int primary key auto_increment, name varchar(16), sex enum('male','female') not null default 'male', age int, dep_id int ); 再插入数据: insert into dep values (200,'技术'), (201,'人力资源'), (202,'销售'), (203,'运营'); insert into emp_1(name,sex,age,dep_id) values ('jason','male',18,200), ('egon','female',48,201), ('keven','male',38,201), ('owen','male',15,202), ('jerry','male',25,204);
了解:
笛卡尔积: 左表的一条记录与右表所有记录都对应一遍 select * from emp_1,dep;
1、内连接:只取两张表有对应关系的记录 inner join
select * from emp_1 inner join dep on emp_1.dep_id=dep.id;
2、左连接:在内连接的基础上保留左表没有对应关系的记录 left join
select * from emp_1 left join dep on emp_1.dep_id=dep.id;
3、右连接:在内连接的基础上保留右表没有对应关系的记录 right join
select * from emp_1 right join dep on emp_1.dep_id=dep.id;
4、全连接:在内连接的基础上保留左、右面表没有对应关系的的记录 union
select * from emp_1 left join dep on emp_1.dep_id=dep.id; union select * from emp_1 right join dep on emp_1.dep_id=dep.id;
5、子查询
查询部门是技术或者人力资源的员工信息 select * from emp_1 where dep_id in (select id from dep where name in ('技术','人力资源'));