Left join FROM 多个表

Left join FROM multiple tables

这个问题与我看到的已经回答的问题不同。是否可以使用多个 'FROM' 表进行左连接?当我尝试它时(使用其他代码,但原理是相同的)我得到错误“'on clause' 中的未知列 'table1.otherId'”。任何帮助将不胜感激。

像这样:

SELECT * 
FROM table1, table2
LEFT JOIN other_table
ON other_table.id = table2.id
AND other_table.otherId = table1.otherId

这样试试:-

SELECT * 
FROM table1
LEFT JOIN other_table
ON table1.id=other_table.id LEFT JOIN table2 ON
other_table.otherId = table2.id

答案是“否”

但是你可以这样做:

SELECT * FROM table2, other_table ON other_table.id = table2.id
LEFT JOIN table1 ON other_table.otherId = table1.otherId

这取决于你想做什么。您似乎想要前两个表的笛卡尔积和第三个表的查找:

SELECT * 
FROM table1 CROSS JOIN
     table2 LEFT JOIN
     other_table
     ON other_table.id = table2.id AND
        other_table.otherId = table1.otherId;

逗号——应该永久禁止出现在 FROM 子句中——类似于 CROSS JOIN。但是,SQL 语句的解析是不同的。逗号防止它之前的表在它之后的 ON 子句中使用;那是你错误的根源。