- 论坛徽章:
- 0
|
写代码的时候会对条件的位置产生疑问,这里给一个总结,避免下次再浪费时间。
1. 对于inner join,条件放在on和where后面是一样的。 2. 对于outer join,条件放在on和where后是不一样的;oracle先过滤on后面的单个表的条件,然后进行join,对join的结果执行where后面的过滤条件。 一下是示例: 先准备测试数据:
- SQL> create table table1(
-
2 id number,
-
3 col1 varchar2(20),
-
4 col2 varchar2(20)
-
5 )
-
6 ;
-
-
Table created
-
-
SQL> create table table2(
-
2 id2 number,
-
3 col3 varchar2(20),
-
4 col4 varchar2(20)
-
5 );
-
-
Table created
-
-
SQL>
-
SQL> begin
-
2 insert into table1 select
-
3 rownum, lpad(chr(rownum+56), 10, chr(rownum+56)), lpad(chr(rownum+56), 10, chr(rownum+56))
-
4 from dual connect by rownum <= 10;
-
5 insert into table2 select
-
6 rownum, lpad(chr(rownum+56), 10, chr(rownum+56)), lpad(chr(rownum+56), 10, chr(rownum+56))
-
7 from dual connect by rownum <= 10;
-
8 delete from table1 where id < 3;
-
9 delete from table2 where id2 > 8;
-
10 commit;
-
11 end;
-
12 /
-
-
PL/SQL procedure successfully completed
-
-
SQL> col col1 format a10;
-
SQL> col col2 format a10;
-
SQL> col col3 format a10;
-
SQL> col col4 format a10;
-
SQL> col id format a4;
-
SQL> col id2 format a4;
-
SQL> select * from table1;
-
-
ID COL1 COL2
-
---- ---------- ----------
-
3 ;;;;;;;;;; ;;;;;;;;;;
-
4 <<<<<<<<<< <<<<<<<<<<
-
5 ========== ==========
-
6 >>>>>>>>>> >>>>>>>>>>
-
7 ?????????? ??????????
-
8 @@@@@@@@@@ @@@@@@@@@@
-
9 AAAAAAAAAA AAAAAAAAAA
-
10 BBBBBBBBBB BBBBBBBBBB
-
-
8 rows selected
-
-
SQL> select * from table2;
-
-
ID2 COL3 COL4
-
---- ---------- ----------
-
1 9999999999 9999999999
-
2 :::::::::: ::::::::::
-
3 ;;;;;;;;;; ;;;;;;;;;;
-
4 <<<<<<<<<< <<<<<<<<<<
-
5 ========== ==========
-
6 >>>>>>>>>> >>>>>>>>>>
-
7 ?????????? ??????????
-
8 @@@@@@@@@@ @@@@@@@@@@
-
-
8 rows selected
(1)执行inner join查询,从结果可以看出,条件的放置位置不影响结果。
- SQL> select * from table1 t1, table2 t2
-
2 where t1.id = t2.id2 and t1.id > 4
-
3 ;
-
-
ID COL1 COL2 ID2 COL3 COL4
-
---- ---------- ---------- ---- ---------- ----------
-
5 ========== ========== 5 ========== ==========
-
6 >>>>>>>>>> >>>>>>>>>> 6 >>>>>>>>>> >>>>>>>>>>
-
7 ?????????? ?????????? 7 ?????????? ??????????
-
8 @@@@@@@@@@ @@@@@@@@@@ 8 @@@@@@@@@@ @@@@@@@@@@
-
-
SQL>
-
SQL> select * from table1 t1
-
2 inner join table2 t2 on t1.id = t2.id2 and t1.id > 4;
-
-
ID COL1 COL2 ID2 COL3 COL4
-
---- ---------- ---------- ---- ---------- ----------
-
5 ========== ========== 5 ========== ==========
-
6 >>>>>>>>>> >>>>>>>>>> 6 >>>>>>>>>> >>>>>>>>>>
-
7 ?????????? ?????????? 7 ?????????? ??????????
-
8 @@@@@@@@@@ @@@@@@@@@@ 8 @@@@@@@@@@ @@@@@@@@@@
(2)下面看outer join:
- SQL> select * from table1 t1
-
2 left join table2 t2 on t1.id = t2.id2 and t2.id2 > 6
-
3 order by id;
-
-
ID COL1 COL2 ID2 COL3 COL4
-
---- ---------- ---------- ---- ---------- ----------
-
3 ;;;;;;;;;; ;;;;;;;;;;
-
4 <<<<<<<<<< <<<<<<<<<<
-
5 ========== ==========
-
6 >>>>>>>>>> >>>>>>>>>>
-
7 ?????????? ?????????? 7 ?????????? ??????????
-
8 @@@@@@@@@@ @@@@@@@@@@ 8 @@@@@@@@@@ @@@@@@@@@@
-
9 AAAAAAAAAA AAAAAAAAAA
-
10 BBBBBBBBBB BBBBBBBBBB
-
-
8 rows selected
-
-
SQL>
-
SQL> select * from table1 t1
-
2 left join table2 t2 on t1.id = t2.id2
-
3 where t2.id2 > 6
-
4 order by id;
-
-
ID COL1 COL2 ID2 COL3 COL4
-
---- ---------- ---------- ---- ---------- ----------
-
7 ?????????? ?????????? 7 ?????????? ??????????
-
8 @@@@@@@@@@ @@@@@@@@@@ 8 @@@@@@@@@@ @@@@@@@@@@
可见,对于后表,在on里的条件和where后面的条件是完全不同的。on里的条件会在join前处理,where中的条件会在join后处理。
|
|