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;
第一个 的left join table3 c on a.xxx = c.xxx 表示:a有数据,那么c可能有数据
第二个 的left join table3 c on b.xxx = c.xxx 表示:b有数据,那么c可能有数据
这两个肯定不一样的
第三个你表述的有问题,ab.xxx是哪个表的 xxx???
这个一两句话是说不清楚的。建议你去看看 T-SQL技术内幕的相关内容 里面介绍得很清楚
你可以看看 LEFT JOIN的执行顺序。
最终的结查是一样的, Left Join左表所有数都在,如果你把表的顺序改了,应该 就不一样了。你理解的没问题的。