left join与right join

create table mytest1(id number,name varchar2(20));
insert into mytest1 select 1,'a' from dual;
insert into mytest1 select 2,'b' from dual;
insert into mytest1 select 3,'c' from dual;
insert into mytest1 select null,'d' from dual;

create table mytest2(id number,name varchar2(20));
insert into mytest2 select 1,'A' from dual;
insert into mytest2 select 2,'B' from dual;
insert into mytest2 select 3,'C' from dual;
insert into mytest2 select null,'D' from dual;

create table mytest3(id number,name varchar2(20));
insert into mytest3 select 11,'a' from dual;
insert into mytest3 select 22,'b' from dual;
insert into mytest3 select 33,'c' from dual;
insert into mytest3 select null,'d' from dual;
commit;

======================================================

SQL> select * from mytest1 join mytest2 on(mytest1.id=mytest2.id);

        ID NAME                         ID NAME
---------- -------------------- ---------- --------------------
         1 a                             1 A
         2 b                             2 B
         3 c                             3 C

SQL> select * from mytest1,mytest2 where mytest1.id=mytest2.id;

        ID NAME                         ID NAME
---------- -------------------- ---------- --------------------
         1 a                             1 A
         2 b                             2 B
         3 c                             3 C

======================================================

SQL> select * from mytest1 right join mytest2 on(mytest1.id=mytest2.id);

        ID NAME                         ID NAME
---------- -------------------- ---------- --------------------
         1 a                             1 A
         2 b                             2 B
         3 c                             3 C
                                           D

SQL> select * from mytest1,mytest2 where mytest1.id(+)=mytest2.id;

        ID NAME                         ID NAME
---------- -------------------- ---------- --------------------
         1 a                             1 A
         2 b                             2 B
         3 c                             3 C
                                           D

======================================================

SQL> select * from mytest1 left join mytest2 on(mytest1.id=mytest2.id);

        ID NAME                         ID NAME
---------- -------------------- ---------- --------------------
         1 a                             1 A
         2 b                             2 B
         3 c                             3 C
           d

SQL> select * from mytest1,mytest2 where mytest1.id=mytest2.id(+);

        ID NAME                         ID NAME
---------- -------------------- ---------- --------------------
         1 a                             1 A
         2 b                             2 B
         3 c                             3 C
           d

======================================================

SQL> select * from mytest1 left join mytest2 on(mytest1.id=mytest2.id)
  2  left join mytest3 on(mytest1.name=mytest3.name);

        ID NAME                         ID NAME                         ID NAME
---------- -------------------- ---------- -------------------- ---------- --------------------
         1 a                             1 A                            11 a
         2 b                             2 B                            22 b
         3 c                             3 C                            33 c
           d                                                               d
SQL> select * from mytest1,mytest2,mytest3 where mytest1.id=mytest2.id(+) and mytest1.name=mytest3.name(+);

        ID NAME                         ID NAME                         ID NAME
---------- -------------------- ---------- -------------------- ---------- --------------------
         1 a                             1 A                            11 a
         2 b                             2 B                            22 b
         3 c                             3 C                            33 c
           d                                                               d

======================================================

SQL> select * from mytest1 left join mytest2 on(mytest1.id=mytest2.id and mytest2.name='B')
  2  left join mytest3 on(mytest1.name=mytest3.name);

        ID NAME                         ID NAME                         ID NAME
---------- -------------------- ---------- -------------------- ---------- --------------------
         1 a                                                            11 a
         2 b                             2 B                            22 b
         3 c                                                            33 c
           d                                                               d

SQL> select * from mytest1,(select * from mytest2 where name='B') mytt2,mytest3
  2  where mytest1.id=mytt2.id(+)
  3  and mytest1.name=mytest3.name(+);

        ID NAME                         ID NAME                         ID NAME
---------- -------------------- ---------- -------------------- ---------- --------------------
         1 a                                                            11 a
         2 b                             2 B                            22 b
         3 c                                                            33 c
           d                                                               d

SQL> select * from mytest1,mytest2,mytest3
  2  where mytest1.id=mytest2.id(+)
  3  and mytest2.name='B'
  4  and mytest1.name=mytest3.name(+);

        ID NAME                         ID NAME                         ID NAME
---------- -------------------- ---------- -------------------- ---------- --------------------
         2 b                             2 B                            22 b

======================================================

数据库在通过连接两张或多张表来返回记录时,都会生成一张中间的临时表,然后再将这张临时表返回给用户。

在使用left jion时,on和where条件的区别如下:

1、 on条件是在生成临时表时使用的条件,它不管on中的条件是否为真,都会返回左边表中的记录。

2、where条件是在临时表生成好后,再对临时表进行过滤的条件。这时已经没有left join的含义(必须返回左边表的记录)了,条件不为真的

就全部过滤掉。

假设有两张表:

表1 mytest1:

        ID NAME
---------- --------------------
         1 a
         2 b
         3 c
           d

表2 mytest2:

        ID NAME
---------- --------------------
         1 A
         2 B
         3 C
           D


两条SQL:
1、select * from mytest1 left join mytest2 on (mytest1.id=mytest2.id) where mytest2.name='B';
2、select * form. mytest1 left join mytest2 on (mytest1.id = 没有test.id and mytest2.name='B');

第一条SQL的过程:

1、中间表
on条件:
mytest1.id=mytest2.id

        ID NAME                         ID NAME
---------- -------------------- ---------- --------------------
         1 a                             1 A
         2 b                             2 B
         3 c                             3 C
           d

2、再对中间表过滤
where 条件:
mytest2.name='B';

        ID NAME                         ID NAME
---------- -------------------- ---------- --------------------
         2 b                             2 B


第二条SQL的过程:

1、中间表
on条件:
mytest1.id=mytest2.id and mytest2.name='B'

        ID NAME
---------- --------------------
         2 B

2、对结果集进行连接.
(条件不为真也会返回左表中的记录)

        ID NAME                         ID NAME
---------- -------------------- ---------- --------------------
         2 b                             2 B
            d
         3 c
         1 a

其实以上结果的关键原因就是left join,right join,full join的特殊性,不管on上的条件是否为真都会返回left或right表中的记录,

full则具有left和right的特性的并集。 而inner jion没这个特殊性,则条件放在on中和where中,返回的结果集是相同的。

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/11134237/viewspace-672065/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/11134237/viewspace-672065/

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值