sql的左连接和右连接

53 篇文章 1 订阅
34 篇文章 0 订阅
外连接
    使用外连接进行多表查询时,返回的查询结果集中仅包含查询条件和连接条件的行。内连接除了消除了与另一个表中的任何不匹配的行,而外连接扩展了内连接的结果集,除了返回所有匹配的行外,还会返回一部分或全部不匹配的行,这就取决于外连接的类型。
    对于外连接,Oracle中可以使用(+)来表示,也可以使用left right和full outer join关键字。
外连接可以分为下面三类:
左外连接:(left outer join或left join)
右外连接:(right outer join或right join)
全外连接:(full outer join或full join)
    使用外连接,列出与连接条件相匹配的行,并列出左表(左外连接),右表(右表连接)或两个表(全外连接)中,所有符合检索条件的数据行。
事例如下:
我们使用oracle自带的emp和salgrade表进行操作。
salgrade表表示的是工资的等级,每个等级的工资分别有最高值和最低值。emp表为员工的基本信息
SQL> select * from salgrade;
     GRADE      LOSAL      HISAL
---------- ---------- ----------
         1        700       1200
         2       1201       1400
         3       1401       2000
         4       2001       3000
         5       3001       9999
SQL> select empno,ename,sal,grade
  2  from emp e inner join salgrade s
  3  on e.sal between s.losal and s.hisal;


     EMPNO ENAME             SAL      GRADE
---------- ---------- ---------- ----------
      7369 SMITH             800          1
      7876 ADAMS            1100          1
      7900 JAMES             950          1
      7521 WARD             1250          2
      7654 MARTIN           1250          2
      7934 MILLER           1300          2
      7499 ALLEN            1600          3
      7844 TURNER           1500          3
      7566 JONES            2975          4
      7698 BLAKE            2850          4
      7782 CLARK            2450          4
      7788 SCOTT            3000          4
      7902 FORD             3000          4
      7839 KING             5000          5
已选择 14 行。
SQL> insert into emp values(7937,'Candy',null,null,null,500,null,null);//插入的数据中的sal工资为500不在员工的工资范围之内
已创建 1 行。
SQL> insert into salgrade values(6,10000,20000);--插入的等级以及工资也不再范围之内
已创建 1 行。
SQL> select e.empno,e.ename,e.sal,d.grade
  2  from emp e left outer join salgrade d      --使用左联接
  3  on e.sal between d.losal and d.hisal;
     EMPNO ENAME             SAL      GRADE
---------- ---------- ---------- ----------
      7839 KING             5000          5
      7902 FORD             3000          4
      7788 SCOTT            3000          4
      7566 JONES            2975          4
      7698 BLAKE            2850          4
      7782 CLARK            2450          4
      7499 ALLEN            1600          3
      7844 TURNER           1500          3
      7934 MILLER           1300          2
      7521 WARD             1250          2
      7654 MARTIN           1250          2
      7876 ADAMS            1100          1
      7900 JAMES             950          1
      7369 SMITH             800          1
      7937 Candy              500
已选择 15 行。
从上面的数据可以看出,除了查出了所有符合条件的数据之外,我们还查处了不在范围之内的数据      
      7937 Candy              500


右外连接
     右外连接是在结果中除了显示满足条件的行外,还显示join右侧表中所有满足检索条件的行。
select distinct e.deptno,d.deptno from emp e right outer join dept d on e.deptno=d.deptno;
如果使用佳豪实现右外连接,上述的语句等价于下面的语句:
select distinct e.deptno,d.deptno from emp e,dept d where e.deptno(+)=d.deptno;
结果如下:
SQL> select distinct e.deptno,d.deptno from emp e,dept d where e.deptno(+)=d.deptno;


    DEPTNO     DEPTNO
---------- ----------
        30         30
        20         20
        10         10
                   40


SQL> select distinct e.deptno,d.deptno from emp e right outer join dept d on e.deptno=d.deptno;


    DEPTNO     DEPTNO
---------- ----------
        30         30
        20         20
        10         10
                   40


SQL> select e.empno,e.ename,e.sal,d.grade
  2  from emp e right outer join salgrade d
  3  on e.sal between d.losal and d.hisal;
     EMPNO ENAME             SAL      GRADE
---------- ---------- ---------- ----------
      7369 SMITH             800          1
      7876 ADAMS            1100          1
      7900 JAMES             950          1
      7521 WARD             1250          2
      7654 MARTIN           1250          2
      7934 MILLER           1300          2
      7499 ALLEN            1600          3
      7844 TURNER           1500          3
      7566 JONES            2975          4
      7698 BLAKE            2850          4
      7782 CLARK            2450          4
      7788 SCOTT            3000          4
      7902 FORD             3000          4
      7839 KING             5000          5
                                          6  
//工资等级为6的没有数据,但是使用右连接,将salgrade表中不匹配的数据显示出来
已选择 15 行。
SQL> select distinct e.deptno,d.deptno
  2  from emp e right outer join dept d
  3  on e.deptno=d.deptno;
    DEPTNO     DEPTNO
---------- ----------
        30         30
        20         20
        10         10
                    40
SQL> select distinct e.deptno,d.deptno
  2  from emp e,dept d
  3  where e.deptno(+)=d.deptno;  //与上面的结果相同
    DEPTNO     DEPTNO
---------- ----------
        30         30
        20         20
        10         10
                   40

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值