Microsoft Access如何从1个表中显示2个其他表的信息

时间:2014-08-28 19:53:03

标签: sql ms-access report ms-access-2010

我的报告工作有困难。我有一个purchaseOrder表,其中包含1个地址ID,表示送货地址。与purchaseOrder关联的供应商也有一个地址ID,表示帐单邮寄地址。

如果你对我的解释感到困惑,这是关系。 Relationship

我想知道如何告诉我的报告地址记录属于vendor或purchaseOrder。任何建议或帮助将不胜感激。

Report PO

查询的原始SQL,忽略长选择。

SELECT tbl_purchaseOrder.ID, tbl_purchaseOrder.purchaseOrderDate, tbl_vendor.salesContact,   tbl_vendor.phone, tbl_vendor.extension, tbl_vendor.fax, tbl_vendor.vendorNo, tbl_vendor.vendorName, tbl_address.address, tbl_address.city, tbl_address.provinceOrState, tbl_address.postalCode, tbl_address.country, tbl_purchaseOrder.shipVIA, tbl_purchaseOrder.FOB, tbl_purchaseOrder.term, tbl_PODetails.quantityOrder, tbl_rawItemList.ItemSerialCode, tbl_PODetails.dueDate, tbl_PODetails.unitCost, tbl_PODetails.unit, tbl_rawItemList.description, tbl_PODetails.specialInstructions, tbl_purchaseOrder.requistionedBy, [unitCost]*[quantityOrder] AS Total
FROM tbl_rawItemList INNER JOIN (((tbl_address INNER JOIN tbl_purchaseOrder ON tbl_address.ID = tbl_purchaseOrder.addressID) INNER JOIN tbl_vendor ON (tbl_vendor.ID = tbl_purchaseOrder.vendorID) AND (tbl_address.ID = tbl_vendor.addressID)) INNER JOIN tbl_PODetails ON tbl_purchaseOrder.ID = tbl_PODetails.purchaseOrderID) ON tbl_rawItemList.ID = tbl_PODetails.rawItemListID
WHERE (((tbl_purchaseOrder.ID)=[Enter a PO number:]));

1 个答案:

答案 0 :(得分:1)

您真正需要做的是链接到tbl_Address两次,如下所示:

enter image description here

以下是您的FROM子句应该是什么样子:

FROM ((((tbl_PODetails 
INNER JOIN tbl_rawItemList ON tbl_PODetails.rawItemListID = tbl_rawItemList.ID) 
INNER JOIN tbl_purchaseOrder ON tbl_PODetails.purchaseOrderID = tbl_purchaseOrder.ID) 
INNER JOIN tbl_vendor ON tbl_purchaseOrder.VendorID = tbl_vendor.ID) 
INNER JOIN tbl_address AS POAddress ON tbl_purchaseOrder.AddressID = POAddress.ID) 
INNER JOIN tbl_address AS VendorAddress ON tbl_vendor.AddressID = VendorAddress.ID;

请注意,我要链接到tbl_address两次,我会给他们POAddressVendorAddress的别名。因此,要获取供应商的地址信息,您可以将控件绑定到:VendorAddress.addressVendorAddress.city等。

您在SELECT语句中的地址字段必须更改为:

POAddress.address, POAddress.city, POAddress.provinceOrState, POAddress.postalCode, POAddress.country, VendorAddress.address, VendorAddress.city, VendorAddress.provinceOrState, VendorAddress.postalCode, VendorAddress.country

因此,您的完整查询应如下所示:

SELECT tbl_purchaseOrder.ID, tbl_purchaseOrder.purchaseOrderDate, 
       tbl_vendor.salesContact, tbl_vendor.phone, tbl_vendor.extension, 
       tbl_vendor.fax, tbl_vendor.vendorNo, tbl_vendor.vendorName, 
       tbl_purchaseOrder.shipVIA, tbl_purchaseOrder.FOB, tbl_purchaseOrder.term,   
       tbl_PODetails.quantityOrder, tbl_rawItemList.ItemSerialCode, 
       tbl_PODetails.dueDate, tbl_PODetails.unitCost, tbl_PODetails.unit, 
       tbl_rawItemList.description, tbl_PODetails.specialInstructions, 
       tbl_purchaseOrder.requistionedBy, [unitCost]*[quantityOrder] AS Total, 
       POAddress.address, POAddress.city, POAddress.provinceOrState, 
       POAddress.postalCode, POAddress.country, VendorAddress.address, 
       VendorAddress.city, VendorAddress.provinceOrState, 
       VendorAddress.postalCode, VendorAddress.country
FROM ((((tbl_PODetails 
     INNER JOIN tbl_rawItemList ON tbl_PODetails.rawItemListID = tbl_rawItemList.ID) 
     INNER JOIN tbl_purchaseOrder ON tbl_PODetails.purchaseOrderID = tbl_purchaseOrder.ID) 
     INNER JOIN tbl_vendor ON tbl_purchaseOrder.VendorID = tbl_vendor.ID) 
     INNER JOIN tbl_address AS POAddress ON tbl_purchaseOrder.AddressID = POAddress.ID) 
     INNER JOIN tbl_address AS VendorAddress ON tbl_vendor.AddressID = VendorAddress.ID;
WHERE (((tbl_purchaseOrder.ID)=[Enter a PO number:]));