ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

一对一关系的补充

2019-07-03 20:43:17  阅读:200  来源: 互联网

标签:关系 dep group 一对一 补充 emp post id select


单表查询
一、语法顺序
select distinct 查询字段1,查询字段2,。。。 from 表名
where 分组之前的过滤条件
having 分组之后的过滤条件
group by 分组依据
order by 排序字段
limit 显示的条数;

二、执行顺序

select distinct 查询字段1,查询字段2,。。。 from 表名
首先执行 from 确定执行的是那个文件
然后再执行 where 确定分组之前的过滤条件
然后再执行 group by 分组
然后再执行 select 筛选出来
然后再执行 order by 排个序
然后再执行 limit 限制结果的显示条数

三、按照优先级的级别写SQL语句
a、先确定是哪张表 from db39.emp
b、是否有过滤条件 where name like '%i%'
。。。
z、放功能 select

四、where过滤
where字句中可以使用:
1. 比较运算符:> < >= <= <> != #不等于用 != 不用 <>
select id,name from db39.emp where id >= 3 and id <= 6

2. between 80 and 100
select * from db39.emp where id between 3 and 6; # >=3 and <=6

3. in(80,90,100) 值是80或90或100
select * from emp where salary in (20000,18000,17000); # select * from emp where salary = 20000 or salary = 18000 or salary = 17000;

4. like 'egon%', pattern可以是%或_, %表示任意多字符, _表示一个字符
select name,salary from db39.emp where name like '%i%' #要求:查询员工姓名中包含i字母的员工姓名与其薪资
select name,salary from db39.emp where name like '____'; #要求:查询员工姓名是由四个字符组成的的员工姓名与其薪资
select name,salary from db39.emp where char_length(name) = 4; #结果与上一条一致

5. 逻辑运算符:在多个条件直接可以使用逻辑运算符 and or not
select * from db39.emp where id not between 3 and 6;
select * from emp where salary not in (20000,18000,17000);

要求:查询岗位描述为空的员工名与岗位名
select name,post from db39.emp where post_comment is NULL; #针对NULL必须用is,不能用=
select name,post from db39.emp where post_comment is not NULL;
#NULL指的是不占任何存储空间,在mysql中空字符串也是占存储空间的,即不为空(NULL)

五、group by分组
如果不设置成only_full_group_by模式,分完组后用*默认取出的是组内的第一个人的数据。但分完组后单独取组内的某个元素是没有意义的,所以,分组前,一般会对模式做如下处理
#设置sql_mode为only_full_group_by,意味着以后但凡分组,只能取到分组的依据
mysql> set global sql_mode="strict_trans_tables,only_full_group_by";

#聚合函数 group function(一般与分组连用)
select post,max(salary) from emp group by post; #取不出组内的元素name, age..,只能取组名(分组依据)或用聚合函数

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;

#group_concat(分组之后用):把想要用的信息取出;字符串拼接操作
select post,group_concat(name) from emp group by post;
select post,group_concat(name,"_SB") from emp group by post;
select post,group_concat(name,": ",salary) from emp group by post;
select post,group_concat(salary) from emp group by post;

# 补充concat(不分组时用):字符串拼接操作
select concat("NAME: ",name) as 姓名,concat("SAL: ",salary) as 薪资 from emp;

# 补充as语法:为字段或表取别名
select name as 姓名,salary as 薪资 from emp; # as可省略
mysql> select emp.id,emp.name from emp as t1; # 报错
mysql> select t1.id,t1.name from emp as t1; # 同 mysql> select id,name from emp as t1;

# 查询四则运算
select name,salary*12 as annual_salary from emp;

#分组练习
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 sex,avg(salary) from emp group by sex; #查询男员工与男员工的平均薪资,女员工与女员工的平均薪资

select post,avg(salary) from emp where age >= 30 group by post; #统计各部门年龄在30岁以上的员工平均工资

六、having过滤 (一定要用组名(分组依据)或聚合函数)
having的语法格式与where一模一样,只不过having是在分组之后进行的进一步过滤
即where不能用聚合函数,而having是可以用聚合函数,这也是他们俩最大的区别

#统计各部门年龄在30岁以上的员工平均工资,并且保留平均工资大于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去重 (在having之后执行,和post,name等属于同一执行级别)
select distinct post,avg(salary) from emp where age >= 30 group by post having avg(salary) > 10000;

八、order by 排序 (默认升序)
select * from emp order by salary asc; #默认升序排
select * from emp order by salary desc; #降序排
select * from emp order by age desc; #降序排
select * from emp order by age desc,salary asc; #先按照age降序排,再按照薪资升序排

# 统计各部门年龄在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 3;
select * from emp order by salary desc limit 1; #显示薪资最高人的信息
select * from emp limit 0,5; #分页, 从0开始,取5条(1-5)
select * from emp limit 5,5; #分页, 从5开始,取5条(6-10)
分页公式:
如果将每一个的信息条数记为 count
那么第 n 也的信息也就是在总信息中的 第(n-1) * count 条开始,往后取 count 条
也就是
select * from emp limit (n-1)*count ,n;

十、正则表达式
select * from emp where name regexp '^jin.*(n|g)$'; #调正则;正则表达式通用

多表连接查询
一、笛卡尔积
from emp,dep,dep2,...

二、内连接:把两张表有对应关系的记录连接成一张虚拟表
select * from emp inner join dep on emp.dep_id = dep.id;

#应用:
select * from emp,dep where emp.dep_id = dep.id and dep.name = "技术"; # 不推荐;不要用where做连表的活
select * from emp inner join dep on emp.dep_id = dep.id where dep.name = "技术"; #逻辑与上一条一致

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

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

五、全连接:在内连接的基础上,保留左、右边没有对应关系的记录
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;

六、多表连接可以是单表不断地与虚拟表连接
#查找各部门最高工资
select t1.* from emp as t1
inner join
(select post,max(salary) as ms from emp group by post) as t2 #把虚拟表提成t2
on t1.post = t2.post
where t1.salary = t2.ms
;

select t1.* from emp as t1
inner join
(select post,max(salary) as ms from emp group by post) as t2
on t1.salary = t2.ms
;
子查询(一个问题一个问题解决)
把一个查询语句用括号括起来,当做另外一条查询语句的条件去用,称为子查询

select name from emp where dep_id = (select id from dep where name="技术"); #子查询
select emp.name from emp inner join dep on emp.dep_id = dep.id where dep.name="技术"; #链表

#查询平均年龄在25岁以上的部门名
select name from dep where id in (select dep_id from emp group by dep_id having avg(age) > 25); #子查询
select dep.name from emp inner join dep on emp.dep_id = dep.id group by dep.name having avg(age) > 25; #链表

#查看不足2人的部门名(子查询得到的是有人的部门id)
select * from emp where exists (select id from dep where id > 3); #exists用法,当()返回True时,外层查询语句将进行查询;当返回值为False时,外层查询语句不进行查询(empty set)

#查询每个部门最新入职的那位员工
select t1.id,t1.name,t1.post,t1.hire_date,t2.post,t2.max_date 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;




标签:关系,dep,group,一对一,补充,emp,post,id,select
来源: https://www.cnblogs.com/1832921tongjieducn/p/11128865.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有