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');
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是一個(gè)左外連接,然后進(jìn)行where過濾。仔細(xì)分析這個(gè)SQL會(huì)發(fā)現(xiàn),最后的結(jié)果不是所期望的,custom_options表中不符合條件的記錄本來是以null表示的,由于where中的過濾,導(dǎo)致查詢出來的記錄為null的部分都沒有查詢出來。這個(gè)左外連接就和內(nèi)連接沒有任何區(qū)別了。
第二個(gè)SQL語(yǔ)句就可以滿足要求。做連接的時(shí)候就過濾了右邊的一些記錄,這樣就算右表不符合條件的左表記錄也可以查詢出來。