create table emp(
id int primary key 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
('mary','female',18,'20170301','teacher',7300.33,401,1), #以下是教学部
('tom','male',78,'20150302','teacher',1000000.31,401,1),
('kevin','male',81,'20130305','teacher',8300,401,1),
('tony','male',73,'20140701','teacher',3500,401,1),
('owen','male',28,'20121101','teacher',2100,401,1),
('jack','female',18,'20110211','teacher',9000,401,1),
('jenny','male',18,'19000301','teacher',30000,401,1),
('sank','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; # *代表所有字段
select id,name from emp;
1.select:用来指定表的字段数据
2.from:后面跟需要查询的表名
3.where:后面跟筛选条件,用来筛选数据
# 1.查询id大于等于3小于等于6的数据
select id,name from emp where id >= 3 and id <= 6;
select * 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 * from emp where salary in (20000,18000,17000); # 简写
# 3.查询姓名中带有字母o的员工姓名和薪资
select name,salary from emp where name like '%o%';
"""
模糊查询
关键字:like
关键符号:
%:匹配任意个数的任意字符
_:匹配单个个数的任意字符
eg:show variables like '%mode%';
"""
# 4.查询姓名由四个字符组成的员工姓名和薪资
select name,salary from emp where name like '____';
select name,salary 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 * from emp where salary not in (20000,18000,17000);
# 7.查询岗位描述为空的员工名与岗位名 针对null不能用等号,只能用is
select name,post from emp where post_comment = NULL; # 查询为空!
select name,post from emp where post_comment is NULL;
select name,post from emp where post_comment is not NULL;
分组就是将单个的个体按照指定的条件分成一个个整体。
注意:分组之后默认只能直接获取到分组的依据,其他字段无法再直接获取(但可以间接获取)。
# 严格模式
set global sql_mode='STRICT_TRANS_TABLES,PAD_CHAR_TO_FULL_LENGTH,only_full_group_by'
# 1.每个部门的最高薪资
select post,max(salary) from emp group by post;
# 2.每个部门的最低薪资
select post,min(salary) from emp group by post;
# 3.每个部门的平均薪资
select post,avg(salary) from emp group by post;
# 4.每个部门的人数
select post,count(id) from emp group by post;
# 5.每个部门的月工资总和
select post,sum(salary) from emp group by post;
"""
可以给字段起别名(as还可以给表起别名)
select post as '部门',sum(salary) as '总和' from emp group by post;
"""
# 查询分组之后的部门名称和每个部门下所有的员工姓名
# group_concat(): 获取分组以外的字段数据 并且支持拼接操作
select post,group_concat(name) from emp group by post;
select post,group_concat(name,':',salary) from emp group by post;
# concat(): 未分组之前使用的拼接功能
select concat(name,':',sex) from emp;
# concat_ws() : 第一个参数为分隔符,将后面的所有字段按照第一个参数分隔
select concat_ws(':',name,sex,salary,age) from emp;
# 一般在分组之后使用
max 最大值
min 最小值
sum 求和
count 计数
avg 平均值
在功能上,having与where是一模一样的,但是在使用的位置上有所不同,where关键字在分组之前使用,而having关键字在分组之后使用。
# 1.统计各部门年龄在30岁以上的员工平均工资,并且保留平均工资大于10000的部门
# 1.1先筛选出所有30岁以上的员工
select * from emp where age>30;
# 1.2然后再按照部门分组
'''SQL语句的查询结果我们也可以直接看成是一张表'''
select post,avg(salary) from emp where age>30 group by post;
# 1.3分组之后做过滤操作
select post,avg(salary) from emp
where age>30
group by post
having avg(salary)>10000
;
去重有一个非常严格的前提条件,数据必须是完全一样,才会被去重,比如说数据带有主键,那么肯定是无法去重的,因为主键是唯一的。
select distinct age from emp;
select * from emp order by salary; # 默认是升序
select * from emp order by salary asc; # asc 升序关键字,可以不写,默认是asc
select * from emp order by salary desc; # 降序
# 排序也可以指定多个字段
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 avg(salary);
limit关键字的用处是来限制数据的展示条数。
select * from emp limit 5; # 前五条
select * from emp limit 5,5; # 起始位置、条数
# 查询工资最高的人的详细信息
# 先按照工资排序 然后限制展示条数
select * from emp order by salary desc limit 1;
regexp关键字的目的就是用正则表达式去字符串中筛选出符合条件的数据。
select * from emp where name regexp '^j.*(n|y)$';
# '^j.*(n|y)$' j开头 中间无所谓 n或者y结尾
#建表
create table dep(
id int primary key auto_increment,
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,'运营'),
(205,'公关');
insert into emp(name,sex,age,dep_id) values
('jack','male',18,200),
('mary','female',48,201),
('kevin','male',18,201),
('nick','male',28,202),
('owen','male',18,203),
('jerry','female',18,204);
# 查询jack所在的部门名称
# 第一步 先获取jack所在的部门id
select dep_id from emp where name='jack';
# 第二步 根据id号去部门表中筛选
select * from dep where id = 200;
# 完整句式
select * from dep where id=(select dep_id from emp where name='jack');
# 前戏
select * from emp,dep;
# 基于上表筛选数据
'''为了避免字段冲突 可以在字段名前面加表名明确'''
select * from emp,dep where emp.dep_id=dep.id;
# inner join
select * from emp inner join dep on emp.dep_id=dep.id;
# left join
select * from emp left join dep on emp.dep_id=dep.id;
# right join
select * from emp right join dep on emp.dep_id=dep.id;
# 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;