❶ left join 過濾條件寫在on後面和寫在where 後面的區別
left join中on是在聯接表的時候就先對右表進行條件過濾,而where,是在表的串聯及過濾完成之後,再對串聯後的結果進行過濾。
❷ oracle資料庫中,left join後 where 的使用方法
可以這樣理解:on是在生成連接表時起作用的,where是生成連接表之後對內連接表再進行過濾。
1、容 on條件是在生成臨時表時使用的條件,它不管on中的條件是否為真,都會返回左邊表中的記錄。
2、where條件是在臨時表生成好後,再對臨時表進行過濾的條件。這時已經沒有left join的含義(必須返回左邊表的記錄)了,條件不為真的就全部過濾掉。
使用left join時,無論on的條件是否滿足,都會返回左表的所有記錄,對於滿足的條件的記錄,兩個表對應的記錄會連接起來,對於不滿足條件的記錄,那右表欄位全部是null。
❸ left join 過濾條件寫在on後面和寫在where 後面的區別
left join 過濾條件原文如下:
create table t1(id int, feild int);
insert into t1 values(1 , 1);
insert into t1 values(1 , 2);
insert into t1 values(1 , 3);
insert into t1 values(1 , 4);
insert into t1 values(2 , 1);
insert into t1 values(2 , 2);
create table t2(id int, feild int);
insert into t2 values(1 , 1);
insert into t2 values(1 , 2);
insert into t2 values(1 , 5);
insert into t2 values(1 , 6);
insert into t2 values(2 , 1);
insert into t2 values(2 , 3);
select t1.*,t2.* from t1 left join t2 on t1.id=t2.id --取t1表的第一行,掃瞄表,按條件做對比,如果滿足條件,就加入返回結果表.
然後取t1表的第二行,掃瞄t2表,按條件做對比,如果滿足條件,就加入返回結果表.
重復以上過程,直到t1表掃描結束.
select t1.*,t2.* from t1 left join t2 on t1.id=t2.id and t1.feild=1 --給左表加條件的時候,左表滿足條件的,按上面的過程返回值,左表不滿足條件的,直接輸出,右表的列補null
1 1 1 1
1 1 1 2
1 1 1 5
1 1 1 6
2 1 2 1
2 1 2 3
1 2 NULL NULL
1 3 NULL NULL
1 4 NULL NULL
2 2 NULL NULL
select t1.*,t2.* from t1 left join t2 on t1.id=t2.id where t1.feild=1 先執行where後連接查詢
執行where後表為 1 , 1
2 , 1
用它來left join t2.