温故知新之连接查询

一、数据准备:

   1.emp

insert into emp (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO)
values (7369, 'SMITH', 'CLERK', 7902, to_date('17-12-1980', 'dd-mm-yyyy'), 800.00, null, 20);

insert into emp (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO)
values (7499, 'ALLEN', 'SALESMAN', 7698, to_date('20-02-1981', 'dd-mm-yyyy'), 1600.00, 300.00, 30);

insert into emp (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO)
values (7521, 'WARD', 'SALESMAN', 7698, to_date('22-02-1981', 'dd-mm-yyyy'), 1250.00, 500.00, 30);

insert into emp (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO)
values (7566, 'JONES', 'MANAGER', 7839, to_date('02-04-1981', 'dd-mm-yyyy'), 2975.00, null, 20);

insert into emp (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO)
values (7654, 'MARTIN', 'SALESMAN', 7698, to_date('28-09-1981', 'dd-mm-yyyy'), 1250.00, 1400.00, 30);

insert into emp (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO)
values (7698, 'BLAKE', 'MANAGER', 7839, to_date('01-05-1981', 'dd-mm-yyyy'), 2850.00, null, 30);

insert into emp (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO)
values (7782, 'CLARK', 'MANAGER', 7839, to_date('09-06-1981', 'dd-mm-yyyy'), 2450.00, null, 10);

insert into emp (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO)
values (7788, 'SCOTT', 'ANALYST', 7566, to_date('19-04-1987', 'dd-mm-yyyy'), 3000.00, null, 20);

insert into emp (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO)
values (7839, 'KING', 'PRESIDENT', null, to_date('17-11-1981', 'dd-mm-yyyy'), 5000.00, null, 10);

insert into emp (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO)
values (7844, 'TURNER', 'SALESMAN', 7698, to_date('08-09-1981', 'dd-mm-yyyy'), 1500.00, 0.00, 30);

insert into emp (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO)
values (7876, 'ADAMS', 'CLERK', 7788, to_date('23-05-1987', 'dd-mm-yyyy'), 1100.00, null, 20);

insert into emp (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO)
values (7900, 'JAMES', 'CLERK', 7698, to_date('03-12-1981', 'dd-mm-yyyy'), 950.00, null, 30);

insert into emp (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO)
values (7902, 'FORD', 'ANALYST', 7566, to_date('03-12-1981', 'dd-mm-yyyy'), 3000.00, null, 20);

insert into emp (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO)
values (7934, 'MILLER', 'CLERK', 7782, to_date('23-01-1982', 'dd-mm-yyyy'), 1300.00, null, 10);

insert into emp (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO)
values (7935, 'test1', 'CLERK', null, null, null, null, null);

insert into emp (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO)
values (7936, 'test2', 'CLERK', null, null, null, null, null);

insert into emp (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO)
values (7937, 'test3', 'CLERK', null, null, null, null, null);

温故知新之连接查询_第1张图片

   2.dept

insert into dept (DEPTNO, DNAME, LOC)
values (10, 'ACCOUNTING', 'NEW YORK');

insert into dept (DEPTNO, DNAME, LOC)
values (20, 'RESEARCH', 'DALLAS');

insert into dept (DEPTNO, DNAME, LOC)
values (30, 'SALES', 'CHICAGO');

insert into dept (DEPTNO, DNAME, LOC)
values (40, 'OPERATIONS', 'BOSTON');

insert into dept (DEPTNO, DNAME, LOC)
values (50, 'testDept', '');

insert into dept (DEPTNO, DNAME, LOC)
values (60, 'testDept2', '');

insert into dept (DEPTNO, DNAME, LOC)
values (70, 'testDept3', '');


温故知新之连接查询_第2张图片
 二、右外连接

/**
左外连接:
显示连接左边的表(emp)的全部内容,连接右边的表(dept),右表有的话就显示,没有的话就空着
注意,(+)在右边
SELECT e.*, d.dname
  FROM emp e, dept d
 WHERE e.deptno = d.deptno(+)
 ORDER BY d.deptno
**/
SELECT e.*, d.dname
  FROM emp e
  LEFT JOIN dept d ON e.deptno = d.deptno
 ORDER BY d.deptno

 
温故知新之连接查询_第3张图片
 三、右外连接

/**
右外连接:
显示连接右边的表(dept)的全部内容,连接左边的表(emp),左表有的话就显示,没有的话就空着
注意,(+)在左边
SELECT e.*, d.dname
  FROM emp e, dept d
 WHERE e.deptno(+) = d.deptno
 ORDER BY d.deptno
**/
SELECT e.*, d.dname
  FROM emp e
 RIGHT JOIN dept d ON e.deptno = d.deptno
 ORDER BY d.deptno

 
温故知新之连接查询_第4张图片
 四、内连接

/**
内(自)连接:
使用关键字inner join
直接用=
select e.*, d.dname
  from emp e
where  e.deptno=d.deptno
 order by e.empno
**/ 
select e.*, d.dname
  from emp e
 inner join dept d on  e.deptno= d.deptno
 order by e.empno

 
温故知新之连接查询_第5张图片
 五、全连接

select e.*, d.dname
  from emp e
 full join dept d on d.deptno = e.deptno
 order by d.deptno

 
温故知新之连接查询_第6张图片
 

你可能感兴趣的:(温故知新之连接查询)