mysql 单表查询 & 多表查询 & 子查询

时间:2021-06-28 20:43:33   收藏:0   阅读:0

单表查询

多表查询

子查询

#找出平均年龄大于25岁以上的部门
1.普通where写法
select
	d.id,d.name
from
	employee as e,department as d
where
	e.dep_id = d.id
group by
	d.id,d.name
having
	avg(e.age)>25;
2. inner join
select
	d.id,d.name
from
	employee as e inner join department as d on e.dep_id=d.id
group by
	d.id,d.name
having
	avg(e.age)>25;
3.子查询
 (1)先找出平均年龄大于25岁的部门id
select dep_id from employee group by dep_id having avg(age)>25;
 (2)通过部门id,找部门名字
select name from department where id in(201,202);
 (3)综合拼接
select name from department where id in(select dep_id from employee group by dep_id having avg(age)>25);
#查看技术部门员工姓名
1.普通where查询
select
	e.name
from
	department as d,employee as e
where
	d.id = e.dep_id
    and
    d.name = ‘技术‘;
2.inner join写法
select
	e.name
from
	department as inner join demployee as e on d.id = e.dep_id
where
    d.name = ‘技术‘;
3.子查询写法
 (1)找技术部门对应id
    select id from department where name = ‘技术‘;
 (2)通过id找员工姓名
	select name from employee where dep_id = 200;
 (3)综合拼接
	select name from employee where dep_id = (select id from department where name = ‘技术‘);
#查看那个部门没员工
 (1)联表写法(把null的数据露出来,员工的部门dep_id 为null,代表这个部门没人)
select 
	d.id,d.name  
from
	department as d,employee as e on d.id = e.dep_id
where 
	e.dep_id is null
  (2)子查询
	1.先查询,员工在那些部门(所有员工的分类分别是200 201 202 204)
    select dep_id from employee group by dep_id;
    2.把不在部门的数据找出来
    select from department where id not in (200,201,202,204);
    3.联合拼接
    select from department where id not in (select dep_id from employee group by dep_id)
#查询大于平均年龄的员工名与年龄
 1.假设平均年龄是20岁
    select name,age from employee where age >20
 2.找平均年龄
	select avg(age) from employee;
 3.综合拼装
	select name,age from employee where age >(select avg(age) from employee);
  
#把大于其本部门平均年龄的员工名和姓名查出来
 1.先计算各部门平均年龄是多少
    select dep_id,avg(age) from employee group by dep_id;
 2.把查询的各部门的平均年龄和过去的employee联表,变成一张更大的表,方便做一次单表查询
	select
    	*
    from
    	employee as t1 inner join (1号查询到的数据) as t2 on t1.dep_id = t2.dep_id
 3.综合拼接
	select
    	*
    from
    	employee as t1 inner join (select dep_id,avg(age) as avg_age from employee group by dep_id) as t2 on t1.dep_id = t2.dep_id
 4.做最后的数据筛选 age > 平均年龄
   select
    	*
    from
    	employee as t1 inner join (select dep_id,avg(age) as avg_age from employee group by dep_id) as t2 on t1.dep_id = t2.dep_id
     where
    	t1.age>t2.avg_age

#查询每个部门最新入职的那位员工 #利用上一套数据表进行查询;
1.找每个部门最大的入职时间
select post,max(hire_date) as max_date from employee group by post  
2.把子查询搜出来的数据和employee联合成一张更大的表,做一次单表查询
select
	t1.emp_name,t1.hire_date
from
	employee as t1 inner join(1号查出来的数据) as t2 on t1.post = t2.post
where
	t1.hire_date = t2.max_date
 3.综合拼接
select
	t1.emp_name,t1.hire_date
from
	employee as t1 inner join(select post,max(hire_date) as max_date from employee group by post) as t2 on t1.post = t2.post
where
	t1.hire_date = t2.max_date
#带EXISTS关键字的子查询
#exists 关键字,表达存在;
#如果内层sql能够查到数据,返回True,外层sql执行查询语句
#如果内层sql能够查到数据,返回False,外层sql不执行查询语句
select * from employee where exists (select * from employee where id = 1)
评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!