2013-08-16 20 views
1

我在Access 2010中編寫了一個sql查詢,並得到一條錯誤消息,說我選擇的字段在多個關係中使用,所以我需要從一個表中選擇。我以爲我有正確的代碼,明確告訴它哪個表可以選擇,但我仍然得到這個錯誤。編碼時出錯(多重關係)SQL

這是我的代碼:

SELECT I.ingredientID, ingredientTypeCode, ingredientName, amount, unitCode 
FROM Ingredient AS I 
INNER JOIN BatchIngredient AS B ON I.ingredientID=B.ingredientID 
ORDER BY ingredientID; 

如果不指定I.ingredientID說,它將從成分拉ingredientID而忽略BatchIngredient?

+1

這兩個表中是否存在更多字段名稱? – William

+0

Just ingredientID包含在兩個表中。 – Jonathan

回答

1

如果ingredientID存在兩個表中,數據庫引擎會發現這個曖昧......

ORDER BY ingredientID 

我想你需要...

ORDER BY I.ingredientID 

我只想繼續前進,添加SELECT子句中的別名也一樣。將每個X替換爲適當的別名。

SELECT I.ingredientID, X.ingredientTypeCode, X.ingredientName, X.amount, X.unitCode 
+0

太棒了!通過添加「I」。進入它的ORDER BY語句。謝謝! – Jonathan