轉自https://blog.csdn.net/l690781365/article/details/76261093
1.首先了解 on 、where 的執行順序以及效率?
from a join b 與 from a, b 產生的臨時表結果集 都是執行笛卡爾積即(select * from a cross join b )兩表的行乘積數。
on :與取得結果集同步進行數據刷選及過濾。
where : 獲得結果集之后,才進行數據刷選及過濾。
執行順序:on在上游,where在中游,having在下游。
案例:1.select * from test_text tx left outer join test_test ts on tx.id =ts.tid; 執行結果:
2.select * from test_text tx left outer join test_test ts on tx.id =ts.tid where tx.id =ts.tid; 結果集如下:
3.select * from test_text tx left outer join test_test ts on tx.id =ts.tid where tx.id =ts.tid having tx.id =5;