Oracle 左連線(+)加號用法及常用語法之間的關係
廢話不多說 直接上code:
--建立測試表結構:create table test_left_a (a varchar2(50),b varchar2(50))create table test_left_b (a varchar2(50),b varchar2(50))--插入測試資料:insert into test_left_a select 'a','21' from dual;commit;insert into test_left_a select 'c','2111' from dual;commit;
insert into test_left_b select 'a','12' from dual;commit;insert into test_left_b select 'b','13' from dual;commit; --實現左連線查詢(不加where)的幾種語法:A: select * from test_left_a a left join test_left_b b on a.a = b.a;
B: select * from test_left_a a, test_left_b b where a.a = b.a(+);
C:select * from test_left_a a inner join test_left_b b on a.a = b.a(+);--實現左連線查詢(加where)的幾種語法: D:select * from test_left_a a left join test_left_b b on a.a = b.a where a.a = b.a;
E:select * from test_left_a a, test_left_b b where a.a = b.a(+) and a.a = b.a;
F:select * from test_left_a a inner join test_left_b b on a.a = b.a(+) and a.a = b.a;G:select * from test_left_a a inner join test_left_b b on a.a = b.a(+) where a.a = b.a;
測試結論: 以上幾種查詢(暫不考慮效能,只考慮用法)A等價於B等價於C 查詢結果:a 21 a 12c 2111 D等價於E等價於F等價於G查詢結果:a 21 a 12
注意:1.使用inner join的時候 直接在on後面寫條件和增加where後再寫條件是一樣的,原因是內連線是匹配出on條件為真的記錄(參考F和G)2.使用left join或者right join的時候,直接在on後面寫條件和增加where後再寫條件是不一樣的,原因是left join即使on後面的條件為假也會顯示出左表的所有記錄right join即使on後面的條件為假也會顯示出右表的所有記錄(這也是D語句中需要用where的原因)