2013-12-17 56 views
0

我收到以下錯誤消息......不明確的外連接?

「的SQL語句不能被執行,因爲它包含模棱兩可的外部聯接。要強制將先進行一個連接,創建一個執行第一個單獨的查詢加入並在SQL語句中包含該查詢。「

如何修復此錯誤?如果我將「LEFT JOIN」替換爲「INNER JOIN」,錯誤消失,但所需的操作未完成。

這裏是我的代碼:

SELECT route.productfam, 
     facility.location, 
     asmlines.line, 
     tableconsolidate2.sumofyr, 
     tableconsolidate2.sumofyr0, 
     tableconsolidate2.sumofyr1, 
     tableconsolidate2.sumofyr2, 
     tableconsolidate2.sumofyr3, 
     tableconsolidate2.sumofyr4, 
     tableconsolidate2.sumofyr5, 
     route.cycletime, 
     route.numperprod, 
     facilitylines.operationalhr, 
     [18months].[month 1], 
     [18months].[month 2], 
     [18months].[month 3], 
     [18months].[month 4], 
     [18months].[month 5], 
     [18months].[month 6], 
     [18months].[month 7], 
     [18months].[month 8], 
     [18months].[month 9], 
     [18months].[month 10], 
     [18months].[month 11], 
     [18months].[month 12], 
     [18months].[month 13], 
     [18months].[month 14], 
     [18months].[month 15], 
     [18months].[month 16], 
     [18months].[month 17], 
     [18months].[month 18] 
FROM ((productfamily 
     INNER JOIN (facility 
        INNER JOIN tableconsolidate2 
          ON facility.location = 
           tableconsolidate2.[build plant]) 
       ON productfamily.productfamily = 
        tableconsolidate2.[prod series]) 
     LEFT JOIN 18months 
       ON (facility.location = [18months].location) 
        AND (productfamily.productfamily = [18months].[item type])) 
     INNER JOIN ((asmlines 
        INNER JOIN facilitylines 
          ON asmlines.line = facilitylines.line) 
        INNER JOIN route 
          ON asmlines.line = route.line) 
       ON (productfamily.productfamily = route.productfam) 
        AND (facility.location = facilitylines.facility) 

GROUP BY route.productfam, facility.location, Asmlines.line, tableconsolidate2.SumOfyr, tableconsolidate2.SumOfyr0, tableconsolidate2.SumOfyr1, tableconsolidate2.SumOfyr2, tableconsolidate2.SumOfyr3, tableconsolidate2.SumOfyr4, tableconsolidate2.SumOfyr5, route.cycletime, route.numperprod, facilitylines.operationalhr, [18Months].[Month 1], [18Months].[Month 2], [18Months].[Month 3], [18Months].[Month 4], [18Months].[Month 5], [18Months].[Month 6], [18Months].[Month 7], [18Months].[Month 8], [18Months].[Month 9], [18Months].[Month 10], [18Months].[Month 11], [18Months].[Month 12], [18Months].[Month 13], [18Months].[Month 14], [18Months].[Month 15], [18Months].[Month 16], [18Months].[Month 17], [18Months].[Month 18], route.productfam 

ORDER BY facility.location; 
+0

感謝您的編輯...我不知道如何把它放在代碼框。 :D – Ben

+0

我認爲你應該給每個表和子查詢別名 –

+0

我已經閱讀了幾篇關於此的文章:http://www.access-programmers.co.uk/forums/showthread.php?t = 137883。我曾嘗試將此應用於我的查詢,但無法成功地適應此方法。 – Ben

回答

1

如果您創建一個包含LEFT JOIN和INNER JOIN的查詢,訪問可能無法確定哪個連接操作首先執行。由於結果根據左連接還是內連接先執行而不同,因此Access會顯示錯誤消息:

要更正此錯誤,您必須修改查詢,以便清楚地知道首先執行哪個連接。

因此解決方案可以通過將其分解爲兩個查詢然後將它們加入並進行附加查詢來實現。

查詢1:

SELECT route.productfam, facility.location, Asmlines.line, [18Months].[Month 1], [18Months].[Month 2], [18Months].[Month 3], [18Months].[Month 4], [18Months].[Month 5], [18Months].[Month 6], [18Months].[Month 7], [18Months].[Month 8], [18Months].[Month 9], [18Months].[Month 10], [18Months].[Month 11], [18Months].[Month 12], [18Months].[Month 13], [18Months].[Month 14], [18Months].[Month 15], [18Months].[Month 16], [18Months].[Month 17], [18Months].[Month 18], route.cycletime, route.numperprod, facilitylines.operationalhr 
FROM (facility INNER JOIN (ProductFamily INNER JOIN 18Months ON ProductFamily.productfamily = [18Months].[Item Type]) ON facility.location = [18Months].Location) INNER JOIN ((Asmlines INNER JOIN facilitylines ON Asmlines.line = facilitylines.line) INNER JOIN route ON Asmlines.line = route.line) ON (ProductFamily.productfamily = route.productfam) AND (facility.location = facilitylines.facility) 
GROUP BY route.productfam, facility.location, Asmlines.line, [18Months].[Month 1], [18Months].[Month 2], [18Months].[Month 3], [18Months].[Month 4], [18Months].[Month 5], [18Months].[Month 6], [18Months].[Month 7], [18Months].[Month 8], [18Months].[Month 9], [18Months].[Month 10], [18Months].[Month 11], [18Months].[Month 12], [18Months].[Month 13], [18Months].[Month 14], [18Months].[Month 15], [18Months].[Month 16], [18Months].[Month 17], [18Months].[Month 18], route.cycletime, route.numperprod, facilitylines.operationalhr, route.productfam 
ORDER BY facility.location; 

查詢2:

SELECT route.productfam, facility.location, Asmlines.line, tableconsolidate2.SumOfyr, tableconsolidate2.SumOfyr0, tableconsolidate2.SumOfyr1, tableconsolidate2.SumOfyr2, tableconsolidate2.SumOfyr3, tableconsolidate2.SumOfyr4, tableconsolidate2.SumOfyr5, route.cycletime, route.numperprod, facilitylines.operationalhr 
FROM (ProductFamily INNER JOIN (facility INNER JOIN tableconsolidate2 ON facility.location = tableconsolidate2.[Build Plant]) ON ProductFamily.productfamily = tableconsolidate2.[Prod Series]) INNER JOIN ((Asmlines INNER JOIN facilitylines ON Asmlines.line = facilitylines.line) INNER JOIN route ON Asmlines.line = route.line) ON (ProductFamily.productfamily = route.productfam) AND (facility.location = facilitylines.facility) 
GROUP BY route.productfam, facility.location, Asmlines.line, tableconsolidate2.SumOfyr, tableconsolidate2.SumOfyr0, tableconsolidate2.SumOfyr1, tableconsolidate2.SumOfyr2, tableconsolidate2.SumOfyr3, tableconsolidate2.SumOfyr4, tableconsolidate2.SumOfyr5, route.cycletime, route.numperprod, facilitylines.operationalhr, route.productfam 
ORDER BY facility.location; 

查詢3:

Query 1 LEFT JOIN Query 2 
+0

看起來很合理,不知道它是否正確。 :( –

+0

它解決了這個問題! – Ben

+0

謝謝,你必須等待24小時才能接受你自己的答案 – Ben