select a.xx,b.xx,c.xx from table1 a
left join table2 b on a.xxx = b.xxx
left join table3 c on a.xxx = c.xxx
select a.xx,b.xx,c.xx from table1 a
left join table2 b on a.xxx = b.xxx
left join table3 c on b.xxx = c.xxx
select ab.* from (select a.xx,b.xx from table1 a
left join talbe2 b on a.xxx = b.xxx) ab
left join table3 c on ab.xxx = c.xxx
假設沒有語法錯誤,請問這3種情況查出的數據會是一樣的嘛?
我自己test了一下,數據是一樣的,但是我感覺跟我理解的left join不一樣,不知道是不是我的理解壓根就是有問題的
還請大神賜教,以糾正或者肯定我的理解,謝謝
1 式和 2 式是不一樣的
3 式視取哪個表的 xxx 而定, a 表的就和 1 式一樣,反之與 2 時一樣
看個測試:
create temporary table A
select 1 as a
union select 2
union select 3;
create temporary table B
select 1 as b
union select 2;
create temporary table C
select 1 as c
union select 2
union select 3;
select * from A
left join B on a.a=b.b
left join C on a.a=c.c;
select * from A
left join B on a.a=b.b
left join C on b.b=c.c;
select * from
(select * from A left join B on a.a=b.b) ab
left join C on ab.b=c.c;