1 where 条件查询
准备代码在最下面
在使用MySQL select语句时,可以使用 WHERE 子句来指定查询条件,从 FROM 子句的中间结果中选取适当的数据行,达到数据过滤的效果。
查询id大于等于3小于等于6的数据
between
select * from emp where id >= 3 and id <=6;
#between:两者之间
select * from emp where id between 3 and 6
查询薪资是20000或者18000或者17000的数据
#or: 或者
select * from emp where salary=20000 or salary=18000 or salary=17000;
# in: 在什么里
select * from emp where salary in (20000, 18000, 17000);
like
查询员工姓名中包含o字母 的 员工姓名和薪资
# like: 模糊匹配
# %: 匹配0个或多个任意字符
# _: 匹配一个任意字符
select name, salary from emp where name like "%o%";
查询员工姓名是由四个字符组成的员工姓名与薪资
select name, salary from emp where name like "_____";
# char_length(字段): 获取字段长度
select name, salary from emp where char_length(name) = 4;
not in
查询薪资不在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;
select name,post from emp where post_comment is not null;
2 group by 分组
在 MySQL SELECT 语句中,允许使用 GROUP BY 子句,将结果集中的数据行根据选择列的值进行逻辑分组
以post为分组
select * from emp group by post;
聚合函数:max min sum avg count
获取每个部门的最高工资:max min
select post,max(salay) from emp group by post; #以post分组
#as:起别名:不推荐
select post as '部门',max(salay) as '薪资' from emp group by post;
每个部门的平均工资:avg
select post,avg(salay) from emp group by post;
每个部门的工资总和:sum
select post,sum(salay) from emp group by post;
每个部门的人数:count() 中传任意参数都没问题
select post,count(id) from emp group by post;
select post,count(post) from emp group by post;
查询分组之后的部门名称和每个部门下所有员工的姓名
# group_concat(name): 不仅可以获取分组后的某一个字段,并且可以对字符串进行拼接
select post, group_concat(name) from emp group by post;
mysql> select post, group_concat(name) from emp group by post;
+--------------------------------+------------------------------------------------+
| post | group_concat(name) |
+--------------------------------+------------------------------------------------+
| operation | 程咬铁,程咬铜,程咬银,程咬金,张野 |
| sale | 格格,星星,丁丁,丫丫,歪歪 |
| teacher | sean,大饼,jerry,owen,jason,kevin,egon |
| 张江第一帅形象代言部门 | tank |
+--------------------------------+------------------------------------------------+
# 给每个部门的员工名字前 + NB_
select post, group_concat('NB_', name) from emp group by post;
mysql> select post, group_concat('NB_', name) from emp group by post;
+-----------------------+----------------------------------------------------------+
| post | group_concat('NB_', name) |
+-----------------------+----------------------------------------------------------+
| operation | NB_程咬铁,NB_程咬铜,NB_程咬银,NB_程咬金,NB_张野
| sale | NB_格格,NB_星星,NB_丁丁,NB_丫丫,NB_歪歪
| teacher | NB_sean,NB_大饼,NB_jerry,NB_owen,NB_jason,NB_kevin,NB_egon
| 张江第一帅形象代言部门 | NB_tank
+------------------+---------------------------------------------------------------+
# 拼接部门员工名字+薪资
select post,group_concat(name,":",salay) from emp group by post;
mysql> select post, group_concat(name, ":", salary) from emp group by post;
+-----------------------------------+------------------------------------------------------------------------------------------------------+
| post | group_concat(name, ":", salary) |
+-----------------------------------+------------------------------------------------------------------------------------------------------+
| operation | 程咬铁:17000.00,程咬铜:18000.00,程咬银:19000.00,程咬金:20000.00,张野:10000.13 |
| sale | 格格:4000.33,星星:3000.29,丁丁:1000.37,丫丫:2000.35,歪歪:3000.13 |
| teacher | sean:10000.00,大饼:30000.00,jerry:9000.00,owen:2100.00,jason:3500.00,kevin:8300.00,egon:1000000.31 |
| 张江第一帅形象代言部门 | tank:7300.33 |
+-----------------------------------+------------------------------------------------------------------------------------------------------+
#太多的话可以使用 \G
select * from emp\G
# 补充concat(不分组时用)拼接字符串达到更好的显示效果 as语法使用
select concat('Name: ', name) as '名字', concat('Sal: ', salary) as '薪资' from emp;
+-----------------+-----------------+
| 名字 | 薪资 |
+-----------------+-----------------+
| Name: tank | Sal: 7300.33 |
| Name: egon | Sal: 1000000.31 |
| Name: kevin | Sal: 8300.00 |
| Name: jason | Sal: 3500.00 |
| Name: owen | Sal: 2100.00 |
| Name: jerry | Sal: 9000.00 |
# 求各部门所有员工的年薪
select name, salary * 12 as annual_salary from emp;
+-----------+-------------+
| name | salary * 12 |
+-----------+-------------+
| tank | 87603.96 |
| egon | 12000003.72 |
| kevin | 99600.00 |
| jason | 42000.00 |
练习
# 写查询语句的步骤: 先看需要查哪张表,然后看有没有什么限制条件, 再看需要根据什么分组,最后再看需要查看什么字段!
执行顺序:
from ...> where ...> group by ...> select
#注意: 聚合函数:
1 只能在group by后(执行顺序)使用
2 若查询语句没有group by,则默认整张表就是一个分组。
1 查询岗位名以及岗位包含的所有员工名字
2 查询岗位名以及各岗位内包含的员工个数
3 查询公司内男员工和女员工的个数
4 查询岗位名以及各岗位的平均薪资
5 查询岗位名以及各岗位的最高薪资
6 查询男员工与男员工的平均薪资,女员工与女员工的平均薪资
答案
#1 查询岗位名以及岗位包含的所有员工名字
select post, group_concat(name) from emp group by post;
#2 查询岗位名以及各岗位内包含的员工个数
#方案一:
mysql> select count(name),post from emp group by post;
+-------------+-----------------------------------+
| count(name) | post |
+-------------+-----------------------------------+
| 5 | operation |
| 5 | sale |
| 7 | teacher |
| 1 | 张江第一帅形象代言部门 |
+-------------+-----------------------------------+
#方案二:
mysql> select post,group_concat(name) from emp group by post;
+---------------------------------+------------------------------------------------+
| post | group_concat(name) |
+---------------------------------+------------------------------------------------+
| operation | 程咬铁,程咬铜,程咬银,程咬金,张野 |
| sale | 格格,星星,丁丁,丫丫,歪歪 |
| teacher | sean,大饼,jerry,owen,jason,kevin,egon |
| 张江第一帅形象代言部门 | tank |
+---------------------------------+------------------------------------------------+
#3 查询公司内男员工和女员工的个数
mysql> select sex,count(id) from emp group by sex;
+--------+-----------+
| sex | count(id) |
+--------+-----------+
| male | 10 |
| female | 8 |
+--------+-----------+
#4 查询岗位名以及各岗位的平均薪资
select post, avg(salary) from emp group by post;
#5 查询岗位名以及各岗位的最高薪资
select post, max(salary) from emp group by post;
#6 查询男员工与男员工的平均薪资,女员工与女员工的平均薪资
mysql> select sex,avg(salary) from emp group by sex;
+--------+---------------+
| sex | avg(salary) |
+--------+---------------+
| male | 110920.077000 |
| female | 7250.183750 |
+--------+---------------+
#8 统计各部门年龄在30岁以上的员工平均工资
select post, avg(salary) from emp where age > 30 group by post;
3 having 过滤
1.having与where语法一样,只不过having需要在group by后使用;
2.where 不能使用聚合函数,但having可以;
1、统计各部门年龄在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 排序
# 1、根据薪资进行升序
select * from emp order by salary; # 默认升序
select * from emp order by salary asc; # 指定升序
select * from emp order by salary desc; # 指定降序
# 2、根据年龄进行降序排列
select * from emp order by age desc;
# 3、先按照age升序,再按照salary降序
select * from emp order by age asc, salary desc;
# 4、统计 各部门(分组) 年龄在10岁以上的员工平均工资,并且保留平均工资大于1000的部门,然后对平均工资进行升序序
select post, avg(salary) from emp where age > 10 group by post having avg(salary) > 1000 order by avg(salary);
6 limit 限制结果返回数量
# 应用场景: 类似于博客园首页的数据展示,每一页有固定的数量;
# 1、从第一条开始,获取4条记录;
select * from emp limit 4;
# 2、limit可以有两个参数, 参数1:是限制的开始位置, 参数2:是从开始位置展示的条数;
select * from emp limit 0, 4;
select * from emp limit 4, 4;
# 3、查询工资最高的人的详细信息
select * from emp order by salary limit 1;
7 正则
# 在编程中,凡是看到reg开头的,基本上都是跟正则有关
select * from emp where name regexp '^程.*(金|银|铜|铁)$';
8 多表查询
关联查询
# 左表的一条记录与右表的一条记录都对应一遍称之为 -–> "笛卡尔积" PS: 百度科普
# 将所有的数据都对应了一遍,虽然不合理但是其中有合理的数据,现在我们需要做的就是找出合理的数据
# 一 比较麻烦的表关联
1、查询员工以及所在部门的信息;
# 将两张表合并,并且根据id字段去判断
select * from emp2, dep2 where emp2.dep_id = dep2.id;
2、查询部门为技术部的员工及部门信息
select * from emp2, dep2 where emp2.dep_id = dep2.id and dep2.name = '技术';
# 二 将两张表关联到一起的操作,有专门对应的方法
1、inner join
# 1、内连接:只取两张表有对应关系的记录
select * from emp2 inner join dep2 on emp2.dep_id = dep2.id;
select * from emp2 inner join dep2 on emp2.dep_id = dep2.id and dep2.name = '技术';
2、left join
# 2、左连接: 在内连接的基础上保留左表没有对应关系的记录
select * from emp2 left join dep2 on emp2.dep_id = dep2.id;
3、right join
# 3、右连接: 在内连接的基础上保留右表没有对应关系的记录
select * from emp2 right join dep2 on emp2.dep_id = dep2.id;
4、union
# 4、全连接:在内连接的基础上保留左、右面表没有对应关系的的记录
select * from emp2 left join dep2 on emp2.dep_id = dep2.id
union
select * from emp2 right join dep2 on emp2.dep_id = dep2.id;
子查询
# 子查询就是将一个查询语句的结果用括号括起来,当做另一个查询语句的条件去用
# 1.查询部门是技术或者人力资源的员工信息
'''
先获取技术部和人力资源的id号,再去员工表里根据前面的id筛选出符合要求的员工信息;
'''
select * from emp2 where dep_id in (select id from dep2 where name='技术' or name='人力资源');
# 2.每个部门最新入职的员工 思路:先查每个部门最新入职的员工,再按部门对应上联表查询
# 查第一张emp表
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;
0 准备代码
#准备代码
# 创建一张部门表
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
('tank','male',17,'20170301','张江第一帅形象代言部门',7300.33,401,1), # 以下是教学部
('egon','male',78,'20150302','teacher',1000000.31,401,1),
('kevin','male',81,'20130305','teacher',8300,401,1),
('jason','male',73,'20140701','teacher',3500,401,1),
('owen','male',28,'20121101','teacher',2100,401,1),
('jerry','female',18,'20110211','teacher',9000,401,1),
('大饼','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);
#多表查询代码
#建表
create table dep2(
id int,
name varchar(20)
);
create table emp2(
id int primary key auto_increment,
name varchar(20),
sex enum('male','female') not null default 'male',
age int,
dep_id int
);
#插入数据
insert into dep2 values
(200,'技术'),
(201,'人力资源'),
(202,'销售'),
(203,'运营');
insert into emp2(name,sex,age,dep_id) values
('tank','male',17,200),
('egon','female',48,201),
('kevin','male',38,201),
('jason','female',28,202),
('owen','male',18,200),
('sean','female',18,204);