MySQL 单表查询,多表查询

知识点回忆:

show variables like '%mode%';  #查看安全模式

现在所接触的三种安全模式:
set global sql_mode='STRICT_TRANS_TABLES';存储数据过长时遇到的


sal_mode='PAD_CHAR_TO_FULL_LENGTH'; char类型在硬盘存着,在终端显示时默认去除尾部空格


sql_mode="only_full_group_by";  分组时遇到的

  

数据准备:
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)
;


补充:如果在windows系统中,插入中文字符,select的结果为空白,可以将所有字符编码统一设置成gbk
create database 当前库名 charset gbk;
执行顺序:
    def select()
        from() 打开文件
        where() 读取每一行并判断是否满足条件
        group() 对数据进行分组
        having() 再分组之后进行过滤  必须用在分组之后
        distinct() 去重
        order() 用于对筛选后的数据 进行排序
        limit() 限制显示的条数
        最后根据select后制定的字段来显示数据

查询语句:
    select [distinct] {*  |  字段名  | 聚合函数 |  表达式 } from 表名
        [where 条件
    group by 字段名
    having 条件
    order by 字段名
    limit 显示的条数
]

1.* 表示所有列都显示
2.也可以手动指定要显示的列  可以是多个
3.distinct 用于去除重复的记录
        只取出完全相同的记录
        当然 你也可以手动指定要显示的列从而来去重
4.表达式  支持四则运算

  

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%';


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

  

group by 分组

# 数据分组应用场景:每个部门的平均薪资,男女比例等

# 1.按部门分组
select * from emp group by post; 
#在设置严格模式 "only_full_group_by" 之前,在终端显示的是每一个组的第一条数据。设置后,会直接报错!!!

设置方式:
set global  sql_mode="strict_trans_tables,only_full_group_by";


# 2.获取每个部门的工资  
以组为单位统计组内数据>>>聚合查询(聚集到一起合成为一个结果)

# 2.1每个部门的最高工资
select post,max(salary) from emp group by post;
# 2.2每个部门的最低工资
select post,min(salary) from emp group by post;
# 2.3每个部门的平均工资
select post,avg(salary) from emp group by post;
# 2.4每个部门的工资总和
select post,sum(salary) from emp group by post;
# 2.5每个部门的人数
select post,count(id) from emp group by post;


# 3.查询分组之后的部门名称和每个部门下所有的学生姓名
# group_concat(分组之后用)不仅可以用来显示分组内的具体信息concat还可以拼接字符串

select post,group_concat(name) from emp group by post;
#会显示post里各组别里的所有name

select post,group_concat(name,': ',salary) from emp group by post;
#终端里会存在这样 name:salary 一列数据。

补充:concat 在不分组时使用,可以拼接字符串达到更好的显示效果 ,可以与as 语法搭配使用

 

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

select name,salary*12 annual_salary from emp;
#上述语句也可以实现,不过不建议这样使用,语意不明确
练习题:


1. 查询岗位名以及岗位包含的所有员工名字
2. 查询岗位名以及各岗位内包含的员工个数
3. 查询公司内男员工和女员工的个数
4. 查询岗位名以及各岗位的平均薪资
5. 查询岗位名以及各岗位的最高薪资
6. 查询岗位名以及各岗位的最低薪资
7. 查询男员工与男员工的平均薪资,女员工与女员工的平均薪资

参考答案:

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、select post,max(salary) from emp group by post;
6、select post,min(salary) from emp group by post;
7、select sex,group_concat(name),avg(salary) from emp
group by sex;

 

补充点:

select id,name,age from emp where max(salary) > 3000; 
#报错:error 1111 (hy000):invalid use of group function

#解释:关键字where 和 group by 同时出现时,group by 必须在where之后,此时max(salary)>300就类似于先分组,然后利用这结果来判断选择满足的数据,此时就会报错

select id,name,age from emp where salary > 3000;
这一步不会出错!

  

8、统计各部门年龄在30岁以上的员工平均工资

select post,avg(salary) from emp where age > 30 group by post;

  

having 过滤

having的语法格式与where一致,只不过having是在分组之后进行的过滤,即where虽然不能用聚合函数,但是having可以!即having是与group by搭配使用。


1、统计各部门年龄在30岁以上的员工平均工资,并且保留平均工资大于10000的部门。
select port ,avg(salary) from emp where age > 30 group by post having avg(salary) > 10000;


#强调:having必须在group by后面使用
select * from emp having avg(salary) > 10000;  # 报错
#对于我这台机器没报错,因为安全模式中没设置为only_full_group_by!!!

  

distinct 去重

select post from emp;                                select distinct post from emp;

                                                              

order by 排序  默认asc 升序


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

#当遇到所找的salary 好几个都是大小一样时,可以再加上一个排序条件
select * from emp order by age desc,salary asc; 

# 统计各部门年龄在10岁以上的员工平均工资,并且保留平均工资大于1000的部门,然后对平均工资进行排序
select port 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; 
#第一个参数表示起始位置,第二个参数表示的是条数,不是索引位置

  

regexp  正则

select * from emp where name regexp '^j.*(n|y)$' ;

#结果是找出以 j 开头,n或者y结尾的所有满足条件的 所对应的数据信息

  

多表查询

创建表先:

create table dep(
id int,
name varchar(20) 
);

create table emp1(
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 emp1(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 emp1,dep where emp1.dep_id = dep.id;

# 查询部门为技术部的员工及部门信息
select * from emp1,dep where emp1.dep_id = dep.id and dep.name = '技术';


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

 # 1、内连接:只取两张表有对应关系的记录
select * from emp1 inner join dep on emp1.dep_id = dep.id;


select * from emp1 inner join dep on emp1.dep_id = dep.id
where dep.name = "技术";

# 2、左连接: 在内连接的基础上保留左表没有对应关系的记录
select * from emp1 left join dep on emp1.dep_id = dep.id;


# 3、右连接: 在内连接的基础上保留右表没有对应关系的记录
select * from emp1 right join dep on emp1.dep_id = dep.id;


# 4、全连接:在内连接的基础上保留左、右面表没有对应关系的的记录
select * from emp1 left join dep on emp1.dep_id = dep.id
union
select * from emp1 right join dep on emp1.dep_id = dep.id;

  

子查询
# 就是将一个查询语句的结果用括号括起来当作另外一个查询语句的条件去用


需求:查询出工资最高的人的信息
先查出最高工资是多少,接着拿着这工资去匹配
select * from emp where salary = (select max(salary) from emp);

  

猜你喜欢

转载自www.cnblogs.com/changwenjun-666/p/10871554.html