用于确定Access 2013中要从中选择哪个表的条件语句

时间:2014-12-03 22:02:12

标签: sql ms-access table-relationships

我正在尝试提出查询以显示会员提供的购买和产品信息,我想知道是否有办法创建条件声明以确定会员购买的产品是否来自服装表,附件表或他们是否从两个表中购买了产品。表格是Product表格中ProductID的决定因素,如果用户没有,但是从服装表格中,ProductID应为0. ProductID通过ProductTypeID连接到服装和附件表格,如果您需要更多信息,请告诉我们,谢谢!

Access中的表格图像为here

这里是SQL视图

SELECT Member.MemberID, Member.FirstName, Member.LastName, 
Purchase.PurchaseDate, LineItem.CalculatedPrice, Product.ProductType
FROM ClothingType 
   INNER JOIN ((AccessoryType INNER JOIN (((Member INNER JOIN Purchase   
   ON Member.MemberID = Purchase.MemberID) 

   INNER JOIN (Product INNER JOIN LineItem ON Product.ProductID = LineItem.ProductID) 
   ON Purchase.PurchaseID = LineItem.PurchaseID) 

   INNER JOIN Accessory ON Product.ProductID = Accessory.ProductTypeID) 
   ON AccessoryType.AccessoryTypeID = Accessory.AccessoryTypeID) 

   INNER JOIN Clothing ON Product.ProductID = Clothing.ProductTypeID) 
   ON ClothingType.ClothingTypeID = Clothing.ClothingTypeID;

1 个答案:

答案 0 :(得分:0)

您需要修改查询以使用左连接。不幸的是,MS Access对JOINS中的括号感到疯狂,所以我的查询可能是错误的。如果Access抱怨JOIN子句中的错误,请在下面评论,我会尽力解决它:

SELECT Member.MemberID, Member.FirstName, Member.LastName, 
Purchase.PurchaseDate, LineItem.CalculatedPrice, Product.ProductType
FROM Member INNER JOIN (Purchase   

   INNER JOIN (Product INNER JOIN (LineItem 

   LEFT JOIN (Accessory LEFT JOIN (AccessoryType 
   LEFT JOIN (Clothing LEFT JOIN ClothingType ON 
              ClothingType.ClothingTypeID = Clothing.ClothingTypeID) 
   ON Product.ProductID = Clothing.ProductTypeID)
   ON AccessoryType.AccessoryTypeID = Accessory.AccessoryTypeID) 
   ON Product.ProductID = Accessory.ProductTypeID)

   ON Product.ProductID = LineItem.ProductID) 
   ON Purchase.PurchaseID = LineItem.PurchaseID) 
   ON Member.MemberID = Purchase.MemberID);

但是,正如我上面提到的,它可能无法正常工作,因为它使用JOIN会导致访问愚蠢。您可能最好双击设计器中的最后四个连接线(Product右侧)并全部使用#34;从左表中选择所有连接线,只选择与右表"选项(在访问条款中为LEFT JOIN

更新:忘记添加:一旦你的联接工作完成,你需要做的就是使用一个表达式的附件/服装描述,如下所示:

 Iif(IsNULL(AccessoryType.Description), ClothingType.Description, AccessoryType.Description) as Description