LEFT JOIN 和JOIN 多表连接
四张表contract 、customer、customer3、customer4
:
这是比较熟悉的3张表的连接
SELECT * FROM test.contract a JOIN test.customer b ON a.Num = b.Num2 JOIN test.customer3 c ON a.num = c.num3;
连接不一定join
后面都是和第一张表的连接。
SELECT * FROM test.contract a JOIN test.customer b ON a.Num = b.Num2 LEFT JOIN test.customer4 d ON b.num2 = d.num4;
SELECT * FROM test.contract a JOIN test.customer b ON a.Num = b.Num2 LEFT JOIN test.customer4 d ON a.num = d.num4;
这句和上句的结果是一样的
注意加粗的地方,我认为是拿a
和 b
的 join
结果集 然后和d
表 left join
,此时 a b
还在作用域里面。 为了避免混淆,可以将a b
的结果集起个别名 tmp
SELECT * from (SELECT * FROM test.contract a JOIN test.customer b ON a.Num = b.Num2) tmp LEFT JOIN test.customer4 d ON tmp.num = d.num4 ;
结果同上。
PS:SQL SERVER多表连接表的连接顺序从左往右
还没有评论,来说两句吧...