数据库表单查询、多表查询、

前期表准备

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)
;

#ps:如果在windows系统中,插入中文字符,select的结果为空白,可以将所有字符编码统一设置成gbk
创建表,插入数据

一、语法的执行顺序

select * from emp\G;

当表字段特别多的时候 结果的排版可能会出现混乱的现象 你可以在查询语句加\G来规范查询结果

# 语法顺序
select
from
where
group by
(having)

# 再识执行顺序
from
where 
group by
(having)
select

二、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.查询id小于3或者大于6的数据
select *  from emp where id not between 3 and 6;

# 3.查询薪资是20000或者18000或者17000的数据
select * from emp where salary = 20000 or salary = 18000 or salary = 17000;
select * from emp where salary in (20000,18000,17000);  # 简写

# 4.查询薪资不在20000,18000,17000范围的数据
select * from emp where salary not in (20000,18000,17000);

# 5.查询员工姓名中包含o字母的员工姓名和薪资
# 在你刚开始接触mysql查询的时候,建议你按照查询的优先级顺序拼写出你的sql语句
"""
先是查哪张表 from emp
再是根据什么条件去查 where name like ‘%o%’
再是对查询出来的数据筛选展示部分 select name,salary
"""
模糊匹配  like
    %:匹配多个任意字符
    _:匹配一个任意字符
select name,salary from emp where name like '%o%';

# 6.查询员工姓名是由四个字符组成的员工姓名与其薪资
select name,salary from emp where name like '____';
select name,salary from emp where char_length(name) = 4;

# 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;

三、group by 分组

1.分组前戏 ——设置严格模式

# 1.按部门分组之分组前戏
select * from emp group by post;  
# 分组后取出的是每个组的第一条数据,也就是每个组从上到下取出第一个人的所有信息
select id,name,sex from emp group by post;  
# 验证,发现还是上面那几个人,只是信息少了,只显示了id ,name,sex而已

1.分组之后应该做到最小单位是组,而不应该再展示组内的单个数据信息,那样就没分组的意义了,比如你以国家分组,直接看国家有哪些就好了,不必要看国家里面的某个人
2.MySQL中分组之后 只能拿到分组的字段信息 无法直接获取其他字段信息,但是你可以通过其他方法(聚合函数)间接的获取
3.上面两个语句如果不报错,取到了数据,那就说明没有设置严格模式,需要去设置严格模式
#设置严格模式: show variables like '%mode%'; set session 当前窗口有效 set global 全局有效 set global sql_mode="strict_trans_tables,only_full_group_by"; # 设置完成后,重新链接客户端,验证一下: select * from emp group by post; # 报错 select id,name,sex from emp group by post; # 报错 select post from emp group by post; # 获取部门信息 #查询详细信息报错,只能查询到分组的信息,说明设置成功 强调:只要分组了,就不能够再“直接”查找到单个数据信息了,只能获取到组名

2.聚合函数 max min avg sum count

以组为单位统计组内数据>>>聚合查询(聚集到一起合成为一个结果)
#在分组后,即select后面或者having后面才能使用

# 每个部门的最高工资 select post,max(salary) from emp group by post; PS:给字段取别名(as也可以省略,但是一般不要这样干) select post as 部门,max(salary) as 最高工资 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; 在统计分组内个数的时候,填写任意非空字段都可以完成计数,推荐使用能够非空且唯一标识数据的字段,比如id字段

# 聚合函数max min sum count avg只能在分组之后才能使用,也就是紧跟着select用或者紧跟着having(分组后的二次where)
select id,name,age from emp where max(salary) > 3000; # 报错!
select max(salary) from emp;
# 正常运行,不分组意味着每一个人都是一组,等运行到max(salary)的时候已经经过where,group by操作了,只不过我们都没有写这些条件

 

3.group_concat 和 concat

group_concat(分组之后用)不仅可以用来显示除分组外字段还有拼接字符串的作用

1.group_concat 显示分组外字符   拼接字符串
#查询分组之后的部门名称和每个部门下所有人的姓名
select post,group_concat(name) from emp group by post;
#在每个人的名字前后拼接字符 select post,group_concat('D_',name,"_SB") from emp group by post;
#group_concat()能够拿到分组后每一个数据指定字段(可以是多个)对应的值 select post,group_concat(name,": ",salary) from emp group by post; 2.concat拼接 as语法使用
不分组时用)就是用来拼接字符串达到更好的显示效果
select name as 姓名,salary as 薪资 from emp;
select concat("NAME: ",name) as 姓名,concat("SAL: ",salary) as 薪资 from emp;
小技巧:
        concat就是用来帮你拼接数据,不分组情况下使用
        group_concat  分组之后使用,可以拼接数据也可以用来显示其他字段信息

# 补充as语法 既可以给字段起别名也可以给表起
select emp.id,emp.name from emp as t1; # 报错  因为表名已经被你改成了t1
select t1.id,t1.name from emp as t1;

3.查询四则运算
# 查询每个人的年薪
select name,salary*12 as annual_salary from emp;
select name,salary*12 annual_salary from emp;  # as可以省略

4.练习题

# 刚开始查询表,一定要按照最基本的步骤,先确定是哪张表,再确定查这张表有没有限制条件,再确定是否需要分类,最后再确定需要什么字段对应的信息

1. 查询岗位名以及岗位包含的所有员工名字
2. 查询岗位名以及各岗位内包含的员工个数
3. 查询公司内男员工和女员工的个数
4. 查询岗位名以及各岗位的平均薪资
5. 查询岗位名以及各岗位的最高薪资
6. 查询岗位名以及各岗位的最低薪资
7. 查询男员工与男员工的平均薪资,女员工与女员工的平均薪资
"""
参考答案:
select post,group_concat(name) from emp group by post;
select post,count(id) from emp group by post;
select sex,count(id) from emp group by sex;
select post,avg(salary) from emp group by post;
select post,max(salary) from emp group by post;
select post,min(salary) from emp group by post;
select sex,avg(salary) from emp group by sex;
"""
8、统计各部门年龄在30岁以上的员工平均工资
select post,avg(salary) from emp where age > 30 group by post; 
# 对where过滤出来的虚拟表进行一个分组
View Code

四、having 筛选

跟where是一模一样的 也是用来筛选数据
        但是having是跟在group by之后的
        where是对整体数据做一个初步的筛选
        而having是对分组之后的数据再进行一次针对性的筛选

1、统计各部门年龄在30岁以上的员工平均工资,并且保留平均工资大于10000的部门 select post,avg(salary) from emp where age > 30 group by post where avg(salary) > 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 id,age from emp; #去重失败,id不一样,即使age一样也没毛用
select distinct post from emp; #成功

六、limit 限制条数

# 限制展示条数
select * from emp limit 5;  # 只展示数据的五条
# 分页显示
select * from emp limit 5,5; #第6条开始,往后展示5条 
    当limit只有一个参数的时候  表示的是只展示几条
    当limit有两个参数的时候   第一个参数表示的起始位置,是索引
    第二个参数表示从起始位置开始往后展示的条数
    
# 查询工资最高的人的详细信息
select * from emp order by salary desc limit 1;

七、regexp 正则

# 在编程中 只要看到reg开头的 基本上都是跟正则相关
select * from emp where name regexp '^j.*(n|y)$';

八、order by 排序

select * from emp order by salary asc; #默认升序排
select * from emp order by salary desc; #降序排

select * from emp order by age desc; #降序排

#先按照age降序排,在年纪相同的情况下再按照薪资升序排
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)
    ;

九、多表查询

#建表
create table dep(
id int,
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,'运营');

insert into emp(name,sex,age,dep_id) values
('jason','male',18,200),
('egon','female',48,201),
('kevin','male',38,201),
('nick','female',28,202),
('owen','male',18,200),
('jerry','female',18,204)
;

# 当初为什么我们要分表,就是为了方便管理,在硬盘上确实是多张表,但是到了内存中我们应该把他们再拼成一张表进行查询才合理
创建表

当初为什么我们要分表,就是为了方便管理,在硬盘上确实是多张表,但是到了内存中我们应该把他们再拼成一张表进行查询才合理

#笛卡尔积

select * from emp,dep;  
# 左表一条记录与右表所有记录都对应一遍,即10*4=40条 >>>笛卡尔积
# 将所有的数据都对应了一遍,虽然不合理但是其中有合理的数据,现在我们需要做的就是找出合理的数据
# 查询员工及所在部门的信息
select * from emp,dep where emp.dep_id = dep.id;
# 查询部门为技术部的员工及部门信息
select * from emp,dep where emp.dep_id = dep.id and dep.name = '技术';

其实将两张表关联到一起的操作,有专门对应的方法:内连接、左连接、右链接、全连接

# 1、内连接:只取两张表有对应关系的记录
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、左连接: 在内连接的基础上保留左表没有对应关系的记录,没有部门信息null补全
select * from emp left join dep on emp.dep_id = dep.id;

# 3、右连接: 在内连接的基础上保留右表没有对应关系的记录,没有员工信息null补全
select * from emp right join dep on emp.dep_id = dep.id;

# 4、全连接:在内连接的基础上保留左、右面表没有对应关系的的记录,空白全用null补全
# 只要将左连接和右连接的sql语句中间加一个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.查询部门是技术或者人力资源的员工信息
"""
先获取技术部和人力资源部的id号,再去员工表里面根据前面的id筛选出符合要求的员工信息
"""
select * from emp where dep_id in (select id from dep where name = "技术" or name = "人力资源");

# 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 max(hire_date) from emp group by post;
#最新日期作为条件
select name,hire_date,post 
from emp where hire_date
in (select max(hire_date) from emp group by post) ;

"""
记住一个规律,表的查询结果可以作为其他表的查询条件,也可以通过其别名的方式把它作为一张虚拟表去跟其他表做关联查询
"""

select * from emp inner join dep on emp.dep_id = dep.id;

 

猜你喜欢

转载自www.cnblogs.com/xp1315458571/p/11391818.html