从两个表生成SQL查询(MS Access)

时间:2015-11-10 21:34:07

标签: sql sql-server database ms-access

我的数据库中有两个表

临时存储订单表

|ID|Date Product Was Made|RecipeID|  Recipe Name  |ProductID|Product Name|
 1   November 10th, 2015    1       100LB S Batch    1         Saltbread
 2   November 10th, 2015    2       100LB B Batch    2         Bran Fruit

食谱表

 |RecipeID|   Recipe Name   |ProductID|Product Name|ItemID| Item |Amount
     1       100LB S Batch       1      Saltbread     213   Salt   100
     1       100LB S Batch       1      Saltbread     56    Sugar   60
     2       100LB B Batch       2      Bran Fruit    213   Salt    34
     2       100LB B Batch       2      Bran Fruit    56    Sugar   12

我想要实现的是显示

的查询表
ItemID | Item |      Date Used      | Amount | ProductID | Product Name |
  213    Salt   November 10th, 2015    100         1         Saltbread
  56     Sugar  November 10th, 2015     60         1         Saltbread
  213    Salt   November 10th, 2015     34         2         Bran Fruit
  56     Sugar  November 10th, 2015     12         2         Bran Fruit

在MS Access中获得此结果的SQL代码是什么

1 个答案:

答案 0 :(得分:0)

这将是您的查询:

--Iso is Interim Storage Orders Table
--Rec is Recipes Table
--As stated in query

SELECT Rec.ItemID, Rec.Item, Iso.DateProductWasMade, 
Rec.Amount, Iso.ProductID, Iso.ProductName
FROM Recipes AS Rec
INNER JOIN InterimStorageOrders AS Iso ON Rec.ProductID = Iso.ProductID