天天看點

sql join 和 left join 差別 on 和 where 差別

有三張表
film      
sql join 和 left join 差別 on 和 where 差別

 category

sql join 和 left join 差別 on 和 where 差別

 film_category

sql join 和 left join 差別 on 和 where 差別

 left join 查詢結果

select *

from film f left join film_category fc on f.film_id=fc.f_id

sql join 和 left join 差別 on 和 where 差別

where  fc.c_id is null

sql join 和 left join 差別 on 和 where 差別

and fc.c_id is null

sql join 和 left join 差別 on 和 where 差別

 join查詢結果

from film f join film_category fc on f.film_id=fc.f_id

sql join 和 left join 差別 on 和 where 差別

where fc.c_id is null

sql join 和 left join 差別 on 和 where 差別

and fc.c_id is null

sql join 和 left join 差別 on 和 where 差別

結論:

1.join 的where 和on沒有差別,内連結,隻有複核條件的才會顯示出來,條件加在那裡都可以

2.left join 的where和on有差別,left join 不管on的條件是否比對都是顯示左表的全部内容,然後形成虛拟表,通過where去篩選臨時表中滿足條件的記錄

如果篩選條件在on就會造成比對不到的資料,左側還是左表的内容,右邊全是null而忽略連結條件 

left join 篩選的主要是右表,如果篩選不到就會在臨時表中顯示null,這裡on加的是連結條件,篩選條件在where中

where不能放函數,函數條件需要放在having中