2012-01-20 7 views
1

ここに私の条件があります。私の状態のアクセスで複数のデータベースを照会する方法

同じingredient.ingIDも参照されている間、私は次の列Recpe表、Recipe.ingIDため

Recipe 
    - rID 
    - pID 
    - ingID 
    - ingAmount 

Product 
    -pID 
    -pName 

Ingredient 
    - ingID 
    - ingName 

と3つの表は、Recipe.pIDは、製品のProduct.pID(主キー)から参照されている

Product.ingIDから。一般的に

Recipe.pID = Product.pID 
Recipe.ingID = Product.pID 
Recipe.ingID = Ingredient.ingID 

私はAccessでちょうど単一のクエリを使用して、次の列を取得します。

pID | pName | ingID | ingName | ingAmount |

Iは、次の試み:

SELECT Recipe.pID, Product.pName, Recipe.ingID, 
     Ingredient.ingName, Recipe.ingAmount 
    FROM Recipe, Product, Ingredient 
WHERE Recipe.pID = 5 
     AND (
      Recipe.ingID = Ingredient.ingID 
      OR Recipe.ingID = Product.pID 
      ); 

問題が(Recipe.ingID = Ingredient.ingID OR Recipe.ingID = Product.pID)一部が複数の行が照会された第1、従って評価されます、です。

私が欲しいものを手に入れたら、私を助けてください。

+0

SELECT DISTINCTを試しましたか?それはあなたが望むことをしますか? – Fionnuala

+0

条件は 'Recipe.ingID = Ingredient.ingIDとRecipe.pID = Product.pID'です。 –

+0

@Florin:いいえ!私はRecipe.ingID = Ingredient.ingIDをtrueにするかRecipe.ingID = product.pIDをtrueにします – user995387

答えて

1
SELECT Recipe.pID, Recipe.ingID, Recipe.ingAmount, 
     Ingredient.ingName AS element_name, 
     'Ingredient' AS element_type 
    FROM Recipe INNER JOIN Ingredient 
      ON Recipe.ingID = Ingredient.ingID 
WHERE Recipe.pID = 5 
UNION 
SELECT Recipe.pID, Recipe.ingID, Recipe.ingAmount, 
     Product.pName AS element_name, 
     'Product' AS element_type 
    FROM Recipe INNER JOIN Product 
      ON Recipe.pID = Product.pID 
WHERE Recipe.pID = 5 
UNION 
SELECT Recipe.pID, Recipe.ingID, Recipe.ingAmount, 
     Product.pName AS element_name, 
     'Product as Ingredient' AS element_type 
    FROM Recipe INNER JOIN Product 
      ON Recipe.ingID = Product.pID 
WHERE Recipe.pID = 5; 
+0

私が探していたものではありません.. – user995387

関連する問題