免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1518 | 回复: 0
打印 上一主题 下一主题

oracle多表连接时条件的位置与结果 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2011-12-22 08:54 |只看该作者 |倒序浏览
写代码的时候会对条件的位置产生疑问,这里给一个总结,避免下次再浪费时间。

1. 对于inner join,条件放在on和where后面是一样的。
2. 对于outer join,条件放在on和where后是不一样的;oracle先过滤on后面的单个表的条件,然后进行join,对join的结果执行where后面的过滤条件。
一下是示例:
先准备测试数据:
  1. SQL> create table table1(
  2.   2 id number,
  3.   3 col1 varchar2(20),
  4.   4 col2 varchar2(20)
  5.   5 )
  6.   6 ;
  7.  
  8. Table created
  9.  
  10. SQL> create table table2(
  11.   2 id2 number,
  12.   3 col3 varchar2(20),
  13.   4 col4 varchar2(20)
  14.   5 );
  15.  
  16. Table created
  17.  
  18. SQL>
  19. SQL> begin
  20.   2 insert into table1 select
  21.   3     rownum, lpad(chr(rownum+56), 10, chr(rownum+56)), lpad(chr(rownum+56), 10, chr(rownum+56))
  22.   4 from dual connect by rownum <= 10;
  23.   5 insert into table2 select
  24.   6     rownum, lpad(chr(rownum+56), 10, chr(rownum+56)), lpad(chr(rownum+56), 10, chr(rownum+56))
  25.   7 from dual connect by rownum <= 10;
  26.   8 delete from table1 where id < 3;
  27.   9 delete from table2 where id2 > 8;
  28.  10 commit;
  29.  11 end;
  30.  12 /
  31.  
  32. PL/SQL procedure successfully completed
  33.  
  34. SQL> col col1 format a10;
  35. SQL> col col2 format a10;
  36. SQL> col col3 format a10;
  37. SQL> col col4 format a10;
  38. SQL> col id format a4;
  39. SQL> col id2 format a4;
  40. SQL> select * from table1;
  41.  
  42.   ID       COL1       COL2
  43. ---- ---------- ----------
  44.    3 ;;;;;;;;;; ;;;;;;;;;;
  45.    4 <<<<<<<<<< <<<<<<<<<<
  46.    5 ========== ==========
  47.    6 >>>>>>>>>> >>>>>>>>>>
  48.    7 ?????????? ??????????
  49.    8 @@@@@@@@@@ @@@@@@@@@@
  50.    9 AAAAAAAAAA AAAAAAAAAA
  51.   10 BBBBBBBBBB BBBBBBBBBB
  52.  
  53. 8 rows selected
  54.  
  55. SQL> select * from table2;
  56.  
  57.  ID2       COL3       COL4
  58. ---- ---------- ----------
  59.    1 9999999999 9999999999
  60.    2 :::::::::: ::::::::::
  61.    3 ;;;;;;;;;; ;;;;;;;;;;
  62.    4 <<<<<<<<<< <<<<<<<<<<
  63.    5 ========== ==========
  64.    6 >>>>>>>>>> >>>>>>>>>>
  65.    7 ?????????? ??????????
  66.    8 @@@@@@@@@@ @@@@@@@@@@
  67.  
  68. 8 rows selected
(1)执行inner join查询,从结果可以看出,条件的放置位置不影响结果。
  1. SQL> select * from table1 t1, table2 t2
  2.   2 where t1.id = t2.id2 and t1.id > 4
  3.   3 ;
  4.  
  5.   ID       COL1       COL2  ID2       COL3       COL4
  6. ---- ---------- ---------- ---- ---------- ----------
  7.    5 ========== ==========    5 ========== ==========
  8.    6 >>>>>>>>>> >>>>>>>>>>    6 >>>>>>>>>> >>>>>>>>>>
  9.    7 ?????????? ??????????    7 ?????????? ??????????
  10.    8 @@@@@@@@@@ @@@@@@@@@@    8 @@@@@@@@@@ @@@@@@@@@@
  11.  
  12. SQL>
  13. SQL> select * from table1 t1
  14.   2 inner join table2 t2 on t1.id = t2.id2 and t1.id > 4;
  15.  
  16.   ID       COL1       COL2  ID2       COL3       COL4
  17. ---- ---------- ---------- ---- ---------- ----------
  18.    5 ========== ==========    5 ========== ==========
  19.    6 >>>>>>>>>> >>>>>>>>>>    6 >>>>>>>>>> >>>>>>>>>>
  20.    7 ?????????? ??????????    7 ?????????? ??????????
  21.    8 @@@@@@@@@@ @@@@@@@@@@    8 @@@@@@@@@@ @@@@@@@@@@
(2)下面看outer join:
  1. SQL> select * from table1 t1
  2.   2 left join table2 t2 on t1.id = t2.id2 and t2.id2 > 6
  3.   3 order by id;
  4.  
  5.   ID       COL1       COL2  ID2       COL3       COL4
  6. ---- ---------- ---------- ---- ---------- ----------
  7.    3 ;;;;;;;;;; ;;;;;;;;;;
  8.    4 <<<<<<<<<< <<<<<<<<<<
  9.    5 ========== ==========
  10.    6 >>>>>>>>>> >>>>>>>>>>
  11.    7 ?????????? ??????????    7 ?????????? ??????????
  12.    8 @@@@@@@@@@ @@@@@@@@@@    8 @@@@@@@@@@ @@@@@@@@@@
  13.    9 AAAAAAAAAA AAAAAAAAAA
  14.   10 BBBBBBBBBB BBBBBBBBBB
  15.  
  16. 8 rows selected
  17.  
  18. SQL>
  19. SQL> select * from table1 t1
  20.   2 left join table2 t2 on t1.id = t2.id2
  21.   3 where t2.id2 > 6
  22.   4 order by id;
  23.  
  24.   ID       COL1       COL2  ID2       COL3       COL4
  25. ---- ---------- ---------- ---- ---------- ----------
  26.    7 ?????????? ??????????    7 ?????????? ??????????
  27.    8 @@@@@@@@@@ @@@@@@@@@@    8 @@@@@@@@@@ @@@@@@@@@@
可见,对于后表,在on里的条件和where后面的条件是完全不同的。on里的条件会在join前处理,where中的条件会在join后处理。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP