1 select sre.*, co.description
2 from subscribedratingelement sre left outer join custom_options co on sre.locationInCdr=co.optionvalue
3 where co.optionname='LocationInCdr';
4 select sre.*, co.description
5 from subscribedratingelement sre left outer join custom_options co
6 on (sre.locationInCdr=co.optionvalue and co.optionname='LocationInCdr');
第一條SQL是一個左外連接,然后進行where過濾。仔細分析這個SQL會發現,最后的結果不是所期望的,custom_options表中不符合條件的記錄本來是以null表示的,由于where中的過濾,導致查詢出來的記錄為null的部分都沒有查詢出來。這個左外連接就和內連接沒有任何區別了。
第二個SQL語句就可以滿足要求。做連接的時候就過濾了右邊的一些記錄,這樣就算右表不符合條件的左表記錄也可以查詢出來。