从多个表中选择字段,并在一个表上使用OUTER JOIN

时间:2011-08-01 06:17:41

标签: mysql join

我的查询从4个表中选择多个字段,我需要添加OUTER JOIN或LEFT OUTER JOIN来比较另一个表中的值。

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

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

2 个答案:

答案 0 :(得分:2)

对所有联接使用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子句看起来都错了。我不知道你正在追究哪个*,而你却错过了逗号

答案 1 :(得分:0)

尝试使用显式JOIN而不是那些隐式的:

SELECT table1.name, table2.wage, table3.shift, table4.vacation
FROM table1
JOIN table2 ON table1.name = table2.name
JOIN table3 ON table2.wage = table3.wage
JOIN table4 ON ??
LEFT JOIN table5 ON table1.name = table5.position

此外,您似乎缺少table4 join的条件。