我的查询从4个表中选择多个字段,我需要添加外部联接或左外部联接来比较另一个表中的值。

SELECT *, table1.name, table2.wage, table3.shift, table4.vacation
FROM table1, table2, table3, table4 OUTER JOIN table5 ON table1.name = table5.position
WHERE table1.name = table2.name
AND table2.wage = table3.wage

这只是一个例子,但我如何使它工作,它似乎工作,如果我只有一个表在从子句。

最佳答案

对所有连接使用ANSI语法,例如

SELECT table1.name, table2.wage, table3.shift, table4.vacation,
  table5.someDateTimeField
FROM table1
INNER JOIN table2 ON table1.name = table2.name
INNER JOIN table3 ON table2.wage = table3.wage
INNER JOIN table4 ON something = table4.something -- can't see this one in your example
LEFT JOIN table5 ON table1.name = table5.position
WHERE someArbitraryFilter BETWEEN 3 AND 7;

而且,您的SELECT子句看起来完全错误。我不知道你要找哪一个

关于mysql - 从多个表中选择字段,然后在一个表上使用OUTER JOIN,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/6894757/

10-15 11:03