2012-01-20 21 views
1

這是我的條件。如何查詢多個數據庫訪問我的病情

我有三個表與表Recpe,Recipe.ingID,Recipe.pID從產品的Product.pID(主鍵)引用下面的列

Recipe 
    - rID 
    - pID 
    - ingID 
    - ingAmount 

Product 
    -pID 
    -pName 

Ingredient 
    - ingID 
    - ingName 

,而同樣ingredient.ingID還引用來自Product.ingID。

一般來說,

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

我想在ACCESS僅使用單個查詢檢索以下列。

pID | pName | ingID | ingName | ingAmount |

我嘗試以下:

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)獲取評估第一因此多行查詢。

如果你有我想問的問題,請幫助我。

+0

您是否試過SELECT DISTINCT?這是否做你想要的? – Fionnuala

+0

條件應該是'Recipe.ingID = Ingredient.ingID和Recipe.pID = Product.pID' –

+0

@弗洛林:不!我想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