一 语法
select distinct 查询字段1,查询字段2,。。。 from 表名
where 分组之前的过滤条件
group by 分组依据
having 分组之后的过滤条件
order by 排序字段
limit 显示的条数;
查询顺序:from----》where---->group by---->having----->distinct----->order by---->limit
def select():
res1=from()
res2=where(res1,pattern)
res3=group(res2,)
res4=having(res3)
res5=distinct(res4)
res6=order(res5)
limit(res6)
二 where过滤
select id,name from db39.emp where id >= 3 and id <= 6
select * from db39.emp where id between 3 and 6;
select * from emp where salary = 20000 or salary = 18000 or salary = 17000;
select * from emp where salary in (20000,18000,17000);
要求:查询员工姓名中包含i字母的员工姓名与其薪资
select name,salary from db39.emp where name like '%i%'
要求:查询员工姓名是由四个字符组成的的员工姓名与其薪资
select name,salary from db39.emp where name like '____';
select name,salary from db39.emp where char_length(name) = 4;
select * from db39.emp where id not between 3 and 6;
select * from emp where salary not in (20000,18000,17000);
要求:查询岗位描述为空的员工名与岗位名
select name,post from db39.emp where post_comment is NULL;
select name,post from db39.emp where post_comment is not NULL;
三 group by分组
#设置sql_mode为only_full_group_by,意味着以后但凡分组,只能取到分组的依据
mysql> set global sql_mode="strict_trans_tables,only_full_group_by";
#每个部门的最高工资
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;
select post,group_concat(name,": ",salary) from emp group by post;
select post,group_concat(salary) from emp group by post;
# 补充concat(不分组时用)
select name as 姓名,salary as 薪资 from emp;
select concat("NAME: ",name) as 姓名,concat("SAL: ",salary) as 薪资 from emp;
# 补充as语法
mysql> select emp.id,emp.name from emp as t1; # 报错
mysql> select t1.id,t1.name from emp as t1;
# 查询四则运算
select name,salary*12 as annual_salary from emp;
分组练习
11 查询python1班 班级id,学生id最大的人id,所有人的名字,把名字列表的列名命名为names
1. 查询岗位名以及岗位包含的所有员工名字
select post,group_concat(name) from emp group by post;
2. 查询岗位名以及各岗位内包含的员工个数
select post,count(id) from emp group by post;
3. 查询公司内男员工和女员工的个数
select sex,count(id) from emp group by sex;
4. 查询岗位名以及各岗位的平均薪资
select post,avg(salary) from emp group by post;
5. 查询岗位名以及各岗位的最高薪资
6. 查询岗位名以及各岗位的最低薪资
7. 查询男员工与男员工的平均薪资,女员工与女员工的平均薪资
select sex,avg(salary) from emp group by sex;
8、统计各部门年龄在30岁以上的员工平均工资
select post,avg(salary) from emp where age >= 30 group by post;
// sum() ---- 求所有数据的总和
// max() ----求所有数据中的最大值
// min() -----求所有数据中的最小值
// count() ---求所有数据的个数
// avg() ---- 求所有数据的平均值
select sname, "同学", age+1, avg(age) from student;
![运行结果]()
![]()
![](https://images2018.cnblogs.com/blog/1354326/201805/1354326-20180511162555822-1304926642.png)
四 having过滤
having的语法格式与where一模一样,只不过having是在分组之后进行的进一步过滤
即where不能用聚合函数,而having是可以用聚合函数,这也是他们俩最大的区别
1、统计各部门年龄在30岁以上的员工平均工资,并且保留平均工资大于10000的部门
select post,avg(salary) from emp
where age >= 30
group by post
having avg(salary) > 10000;
#强调:having必须在group by后面使用
select * from emp
having avg(salary) > 10000;
五 distinct去重
select distinct post,avg(salary) from emp
where age >= 30
group by post
having avg(salary) > 10000;
六 order by 排序
select * from emp order by salary asc; #默认升序排
select * from emp order by salary desc; #降序排
select * from emp order by age desc; #降序排
select * from emp order by age desc,salary asc; #先按照age降序排,再按照薪资升序排
# 统计各部门年龄在10岁以上的员工平均工资,并且保留平均工资大于1000的部门,
然后对平均工资进行排序
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 3;
select * from emp order by salary desc limit 1;
# 分页显示
select * from emp limit 0,5; #从0开始显示5条
select * from emp limit 5,5; #从5开始显示5条
# 正则表达式
select * from emp where name regexp '^jin.*(n|g)$';
二、多表查询
1、内连接:把两张表有对应关系的记录连接成一张虚拟表
select * from emp inner join dep on emp.dep_id = dep.id;
#应用:
select * from emp,dep where emp.dep_id = dep.id and dep.name = "技术"; # 不要用where做连表的活
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 join dep on emp.dep_id = dep.id;
#补充:多表连接可以不断地与虚拟表连接
查找各部门最高工资
select t1.* from emp as t1
inner join
(select post,max(salary) as ms from emp group by post) as t2
on t1.post = t2.post
where t1.salary = t2.ms
;
子查询:
把一条查询语句用括号括起来,用来当作查询条件去使用,这种查询方式就是子查询。
select emp.name from emp inner join dep on emp.dep_id = dep.id where dep.name="技术"; #多表链接查询
select name from emp where dep_id =
(select id from dep where name="技术"); #子查询