day04 查找关键字


day04 查找关键字

昨日内容回顾

基本数据类型之日期相关类型

date	:年月日
time	:时分秒
datetime:年月日时分秒
year	:年

基本数据类型之枚举与集合类型

# 枚举
	多选一(性别)
	enum('male','female')
	
# 集合
	多选多
	set('lookbook','football')

约束条件

1、unsigned:无符号
2、zerofill:0填充
3、not null:非空
4、unique  :唯一
5、default :默认值
6、primary key :主键
7、auto_increment:自增 # 配置主键一起使用

约束条件之外键

# 外键是用来记录表和表之间的关系
# 表关系判断  	    换位思考
	1.多对一
		换位思考之后一边可以一边不可以 则为多对一
        	外键字段建在"多"的一方
    2.多对多
    	换位思考之后两边都可以 则为多对多
        	外键字段建在第三张关系表
       
# SQL
	foreign key(本表外键字段) references 另外一张表名(主键字段)
    on update cascade
    on delete cascade
 
# 外键约束
	1.在创建表的时候先创建被关联表
    2.在录入数据的时候也是先录入被关联表
  
"""
外键虽然可以强行建立表关系 但是会造成表与表之间的强耦合
有时候当表特别多的情况下 可能不会使用外键来维护关系
	而是通过代码层面建立逻辑意义上的关系
"""

今日内容概要

1、表关系之一对一
2、修改表的SQL语句补充
3、表查询关键字
	# 基本关键字
        select
        from
        where
        group by
        having
        distinct
        order by
        limit
        regexp
	# 多表查询关键字
		inner join
		left join
		right join
		union

表关系之一对一

# 场景
	客户表与学生表
	QQ用户表
以用户表和用户详情表为例
	1、先站在用户表的基础之上
		问:一个用户能否对应多个用户详情
		答:不可以
	2、在站在用户详情表基础之上
		问:一个用户详情能否对应对个用户
		答:不可以
	# 结论:换位思考之后两边都不可以,那么表关系有两种
		1、没有关系
		2、一对一关系
		问题:外键字段建在哪呢?
		答:理论上建在热河一方都可以,但是最好建在查询频率较高的表中
		
案例:
    create table user2(
        id int primary key auto_increment,
        name varchar(32),
        detail_id int unique,   # 一对一,不能重复
        foreign key(detail_id) references user_detail(id)
        on update cascade
        on delete cascade
    );
    
    create table user_detail(
    	id int primary key auto_increment,
    	addr varchar(32),
    	phone bigint
    );

操作表的SQL语句补充

1、修改表名称
	alter table m1 rename m2;
	rename table m1 to m2;   # 关键字修改,rename ... to
	rename table m1 to m2, m3 to m4;  # 可以多个修改
	
2、添加表字段
	alter table m1 add pwd int;  # 关键字:add
	alter table m1  add hobby varchar(32) after name;  # 在name字段后面添加hobby字段
	alter table m1 add age int first;  # 在最上面添加字段
	
3、删除表字段
	alter table m1 drop age;
	
4、修改字段名和字段类型
	# modify:修改字段类型
	# change: 同时修改字段类型和字段名称

	alter table m1 modify age tinyint;  # modify:修改字段类型
	alter table m1 change age gender int; # 把age改成gender,并修改字段类型

复制表(了解)

# 查询语句执行的结果也是一张表,可以看成虚拟表

1、复制表结构+记录(key不会复制:主键、外键和索引)
	create table mmm select * from m7;  # 数据都会复制,但是键不会复制
	
2、只拷贝表结构(不包含键)
	create table mqq select * from m7 where 1=2;
	
3、拷贝结构包含各种key
	create table mqq like m7;

单表查询准备

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
('jason','male',18,'20170301','张江第一帅形象代言',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:控制查询的表

where:筛选
                                                                                                     

查找关键字之where

# where其实就是对数据进行筛选

# 模糊查询:
	关键字:like
	关键符号:
		%:匹配任意个数的任意字符
		_:匹配单个个数的任意字符
		
1、查询id大于等于3小于等于6的数据
	select * from emp where id<=3 and id<=6;
	select * from emp where id between 3 and 6;  # between:在两者之间
	
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 naem like '____';
	select name,salary from emp where char_length(name)=4;   # 查询名字是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 * from emp where post_comment is NULL;

查询关键字之group by分组

分组:按照某个指定的条件将单个的数据分为一个个整体
	比如:
		按照年龄分组
		按照省份分组
		按照性别分组

# 格式:
	关键字 group by 条件
    
# as:起别名
# group_concat:用于分组之后获取分组以外的字段数据并支持拼接
# concat:用于分组之前的拼接操作
# concat_ws:当多个字段连接符相同的情况下推荐使用
	
"""
分组之后不再以单个个体为研究对象 也无法直接再获取单个个体的数据
研究对象应该是分组的整体
	分组之后默认只能直接获取到分组的依据 其他字段数据无法直接获取

如果需要实现上述要求 还是修改sql_mode
	set global sql_mode='only_full_group_by';
"""

1、获取每个部分的最大薪资
	select post,max(salary) from emp group by post;
	select post as '部门',max(salary) as '最高薪资' from emp group by post;  # as:起别名
    
2、每个部门的最小薪资
	select post as '部门',min(salary) as '最高薪资' from emp group by post;
    
3、统计每个部门到的人数
	select post as '部门',count(id) as '部门人数' from emp group by post;
    
4、获取每个部门的员工姓名
	# group_concat:用于分组之后获取分组以外的字段数据并支持拼接
	select post,group_concat(name,':',salary) from emp group by post;
    
    # concat:用于分组之前的拼接操作
    select id,concat(name,':',salary) as '呵呵' from emp;
    
    # concat_ws:当多个字段连接符相同的情况下推荐使用
    select id,concat_ws('|',name,post,sex,office) from emp;
    
    '''as可以省略但是为了语义更加明确建议不要省略'''

聚合函数:只能配合group by使用

max() :最大值
min() :最小值
sun() :求和
count() :计数
avg() :求平均数
# 上述聚合函数都是在分组之后使用 用于操作整体数据

练习题

1. 查询岗位名以及岗位包含的所有员工名字
	select post as '岗位',group_concat(name) as '人员' 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,avg(salary) from emp group by sex;

查询关键字之having过滤

where与having都是用来筛选数据的
	where和having的区别:
		where:用于分组之前额筛选
		having:用于分组之后的过滤
		
1、统计各部门年龄在30岁以上的员工平均工资,并且保留平均工资大于10000的部门
	select post,avg(salary) from emp where age > 30 group by post having avg(salary)>10000;

# 解题思路:大象放冰箱
	1、统计各部门的员工平均工资
		select post,avg(salary) from emp group by post;
		
	2、统计30岁以上的员工平均薪资
		select post,avg(salary) from emp where age > 30 group by post;
		
	3、统计各部门年龄在30岁以上的员工平均工资,并且保留平均工资大于10000的部门
		select post,avg(salary) from emp where age > 30 group by post having avg(salary) > 10000;

查询关键字之distinct去重

# 去重的前提是存在一模一样的数据,如果存在主键肯定无法去重

select distinct id,age from emp;  # 有id一定去重不了,因为存有主键
select distinct age from emp;     # 可以去重

查询关键字之order by排序

order by 默认是升序
默认关键字:
	asc :升序
	desc:降序

    select * from emp order by salary asc;  # 工资按升序排列
    select * from emp order by salary desc; # 工资按降序排列

# order by支持多个字段数排序(第一个不行,接着往后排)
    select * from emp order by age,salary asc;
    select * from emp order by age asc,salary desc;