多表查询

多表查询

1. 分类:   

* 合并结果集
    * 连接查询
    * 子查询

合并结果集
  * 要求被合并的表中,列的类型和列数相同,字段可以不相同
  * UNION,去除重复行
  * UNION ALL,不去除重复行

SELECT * FROM cd
UNION ALL
SELECT * FROM ab;

示例:

多表查询

多表查询

连接查询
1. 分类   

* 内连接
    * 外连接
      > 左外连接
      > 右外连接
      > 全外连接(MySQL不支持)
    * 自然连接(属于一种简化方式)

2. 内连接

  * 方言连接:SELECT * FROM 表1 别名1, 表2 别名2 WHERE 别名1.xx=别名2.xx
    * 标准:SELECT * FROM 表1 别名1 INNER JOIN 表2 别名2 ON 别名1.xx=别名2.xx
    * 自然:SELECT * FROM 表1 别名1 NATURAL JOIN 表2 别名2
    * 内连接查询出的所有记录都满足条件。

标准连接:

多表查询

方言连接:

多表查询

自然连接:(不推荐使用,可读性太差

多表查询

输出:

多表查询

3. 外连接  


    * 左外(把左边表的所有条件都查出来,若关联表满足条件则显示出来,如是Null 则关联表全显示 Null):SELECT * FROM 表1 别名1 LEFT OUTER JOIN 表2 别名2 ON 别名1.xx=别名2.xx
      > 左表记录无论是否满足条件都会查询出来,而右表只有满足条件才能出来。左表中不满足条件的记录,右表部分都为NULL

    * 左外自然:SELECT * FROM 表1 别名1 NATURAL LEFT OUTER JOIN 表2 别名2 ON 别名1.xx=别名2.xx

多表查询

多表查询
     

* 右外:SELECT * FROM 表1 别名1 RIGHT OUTER JOIN 表2 别名2 ON 别名1.xx=别名2.xx
      > 右表记录无论是否满足条件都会查询出来,而左表只有满足条件才能出来。右表不满足条件的记录,其左表部分都为NULL
    * 右外自然:SELECT * FROM 表1 别名1 NATURAL RIGHT OUTER JOIN 表2 别名2 ON 别名1.xx=别名2.xx
    * 全链接:可以使用UNION来完成全链接

多表查询

多表查询

 

多表查询

子查询

:查询中有查询(查看select关键字的个数!)
1. 出现的位置: 
    * where后作为条件存在
    * from后作为表存在(多行多列)

多表查询

多表查询 

 

多表查询

多表查询

2. 条件
    * (***)单行单列:SELECT * FROM 表1 别名1 WHERE 列1 [=、>、<、>=、<=、!=] (SELECT 列 FROM 表2 别名2 WHERE 条件)
    * (**)多行单列:SELECT * FROM 表1 别名1 WHERE 列1 [IN, ALL, ANY] (SELECT 列 FROM 表2 别名2 WHERE 条件)
    * (*)单行多列:SELECT * FROM 表1 别名1 WHERE (列1,列2) IN (SELECT 列1, 列2 FROM 表2 别名2 WHERE 条件)
    * (***)多行多列:SELECT * FROM 表1 别名1 , (SELECT ....) 别名2 WHERE 条件

 

练习:数据表

多表查询

1. 查出至少有一个员工的部门。显示部门编号、部门名称、部门位置、部门人数。

select d.deptno,d.dname,d.loc,a.count from dept d,
(select deptno,count(*) count from emp group by deptno) a where d.deptno = a.deptno

多表查询

2.列出薪金比关羽高的所有员工。
select * from emp where sal > (select sal from emp where ename ='关羽')

多表查询

3.列出所有员工的姓名及其直接上级的姓名。

select e.ename,m.ename from emp e,emp m where e.mgr=m.empno

多表查询

4.列出受雇日期早于其直接上级的所有员工的编号,姓名和部门名称

方法一:

select *from 
(select b.ename,b.hiredate,a.ename fname,a.hiredate fhir ,b.deptno from emp a,emp b where a.empno = 
b.mgr and b.hiredate < a.hiredate)a,dept d
where a.deptno = d.deptno

多表查询

方法二:

select e1.empno,e1.ename,d.dname from emp e1 
left join emp e2
on e1.mgr = e2.empno
left join dept d
on e1.deptno = d.deptno
where e1.hiredate < e2.hiredate order by e1.empno 

多表查询

5.列出部门名称和这些部门的员工信息,同时列出那些没有员工的部门。

 select d.dname,e.* from dept d left join emp e on d.deptno=e.deptno

多表查询

6. 列出所有文员的姓名及其部门名称,部门的人数。

select a.ename,a.dname,b.count from 
(select e.ename,d.dname,e.deptno from emp e left join  dept d on e.deptno = d.deptno where e.job ='文员') a
left join 
(select deptno,count(*) count from emp group by deptno) b
on a.deptno = b.deptno

多表查询

7. 列出最低薪金大于15000的各种工作及从事此工作的员工人数。

  select job,count(*)  from emp  group by job  having min(sal)>15000

多表查询

8. 列出在销售部工作的员工的姓名,假定不知道销售部的部门编号。

 select e.ename from emp e  where e.deptno in ( select deptno from dept  where  dname='销售部' )

多表查询

9. 列出薪金高于公司平均薪金的所有员工信息,所在部门名称,上级领导,工资等级。

select a.*,e.ename,s.grade from 
(select e.*,d.dname from emp e, dept d where e.deptno = d.deptno and sal >
(select round(avg(sal),2) from emp)) a
left join emp e on a.mgr = e.empno
left join salgrade s on a.sal between s.losal and s.hisal

多表查询

10.列出与庞统从事相同工作的所有员工及部门名称。

select e.empno,e.ename,d.dname from emp e,dept d  where job in (select distinct job  from emp
  where ename='庞统') and e.deptno=d.deptno and e.ename!='庞统'

多表查询

11.列出薪金高于在部门30工作的所有员工的薪金的员工姓名和薪金、部门名称。

select e.ename,e.sal,d.dname from emp e,dept d where e.deptno = d.deptno and sal>
(select max(sal) from emp where deptno = 30)

多表查询

12.列出在每个部门工作的员工数量、平均工资。

 select d.dname, avg(sal),count(*)from emp e,dept d where e.deptno=d.deptno group by d.dname 

多表查询