是否可以将一个左外连接放在其他左外连接中

时间:2012-05-01 11:31:11

标签: sql db2 left-join join

我可以将左外连接放在另一个左外连接中吗?像这样:

SELECT * FROM table1 
LEFT OUTER JOIN table2 ON (LEFT OUTER JOIN table 3 ON (Join Conditions))
WHERE
....(where conditions)

1 个答案:

答案 0 :(得分:11)

要对多个联接进行分组,语法如下(在db2上未经测试)

SELECT *
FROM table1 t1
LEFT JOIN (
  table2 t2 INNER JOIN table3 t3 ON t3.someId = t2.someId
) ON t2.someId = t1.someId

LEFT JOIN()内左连接的语法相同,但请阅读@ X-Zero的评论

SELECT *
FROM table1 t1
LEFT JOIN (
  table2 t2 LEFT JOIN table3 t3 ON t3.someId = t2.someId
) ON t2.someId = t1.someId