?on and 和 on where 的 區別
在使用?left join?時, on and 和?on where?會有區別;
1. on的條件是在連接生成臨時表時使用的條件,以左表為基準 ,不管on中的條件真否,都會返回左表中的記錄
on 后面 and 都是對右表進行篩選
2.where是全部連接完后,對臨時表進行篩選,篩選對左表和右表都有效
在使用inner join時,on? ?and與on? where條件的區別:不管是對左表還是右表進行篩選,on? and和on? where都會對生成的臨時表進行過濾。
1. 新建表 t1
2. 新建表 t2
3. 關聯查詢:
select * from t1 left join t2 on t1.user_id = t2.user_id
on and?
a.) 對左表加and 條件
select * from t1 left join t2 on t1.user_id = t2.user_id and t1.deleted = 0
?
b.) 對右表加and 條件
select * from t1 left join t2 on t1.user_id = t2.user_id and t2.deleted = 0
?
?on where
select * from t1 left join t2 on t1.user_id = t2.user_id where t1.deleted = 0
select * from t1 left join t2 on t1.user_id = t2.user_id where t2.deleted = 0
?