這幾天在項目中使用到了視圖,感覺還不錯,但是今天發現單純的依靠並表形成的視圖,無法很好的完成特定的任務,比如說有一部分數據不管相關聯的數據有沒有,必須展現出來的(可能我的SQL不是很好,如果哪位達人有好的實現方式,歡迎指教,非常感謝)
於是,我就想到了左連結,並且右表先過濾一次再進行連接
SQL 語句如下:
1
select * from table1 as a left join table2 as b on a.tid=b.tid where a.parentid='' and b.years=2010
2
執行之後,發現還是不太符合要求,查不出數據,於是乎,調整了下代碼:
1
select * from table1 as a left join table2 as b on a.tid=b.tid and b.years=2010
2
where a.parentid=''
1
測試了一下,符合要求的數據呈現出來了,感覺可以把過濾右表的條件放在where 前執行比較靠譜,放在 where後 是對並表後的數據查詢。