在SQL裏做表關聯時,on與where的區別

        left join 和 right join 時,on 的關聯條件只會對關聯表的數據進行過濾,無法對主表的數據進行過濾,若想對主表的數據進行篩選,需要where里加條件進行篩選。

select *
from userInfo a 
    left join roleInfo b on a.userId = b.userId
where a.xxx = ''
--在這個SQL裏面,如果想對a表的數據過濾,只能在where裏面寫條件

        inner join時,on的關聯條件會對主表和關聯表起作用,可以沒有where條件,就能篩選出同時滿足關聯條件的主表和關聯表的數據。

select *
from userInfo a 
    inner join roleInfo b 
    on a.userId = b.userId
    and a.xxx = ''
    and b.xxx = ''
--只在on裏面寫條件也可以篩選出滿足條件的數據

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章