Access中的完全外部联接

时间:2017-03-14 05:59:31

标签: sql-server ms-access join access full-outer-join

我有一个包含FULL OUTER JOIN的查询,我想在Access中实现它。

我知道这可以在访问中使用UNION查询来完成,但我不知道如何。我读了一些文章,但没有理解。这是我想在访问中重写的SQL Server Query。通过描述它是如何工作的,帮助我理解它。

SELECT tblPurchaseMain.Purchase_Date, tblPurchaseDetail.Quantity, tblPurchaseDetail.Rate, 
tblProduct.Product_Name, tblProductCategory.Product_Category_Name, tblSupplier.Supplier_Name,
tblSupplier.Supplier_Address, tblSupplier.Supplier_Phone_No, tblCompany.Company_Name,
tblPurchaseDetail.Amount, tblPurchaseDetail.Discount, tblPurchaseMain.TotalAmount,
tblPurchaseMain.Service_Tax, tblPurchaseDetail.TaxableAmount, 
tblPurchaseDetail.Purchase_Main_ID, tblPurchaseDetail.VAT,
tblPurchaseMain.Purchase_Other_Charges 
FROM 
tblPurchaseDetail
INNER JOIN 
tblPurchaseMain ON tblPurchaseDetail.Purchase_Main_ID = tblPurchaseMain.Purchase_Main_ID
INNER JOIN 
tblProduct ON tblPurchaseDetail.Product_ID = tblProduct.Product_ID 
INNER JOIN 
tblProductCategory ON tblProduct.Product_Category_ID = tblProductCategory.Product_Category_ID
INNER JOIN 
tblSupplier ON tblPurchaseMain.Supplier_ID = tblSupplier.Supplier_ID 
FULL OUTER JOIN 
tblCompany ON tblPurchaseMain.Company_ID = tblCompany.Company_ID 
WHERE 
(tblPurchaseMain.Purchase_Main_ID =" & _PurchaseBillNo & ") 
AND 
(tblPurchaseMain.Company_ID = " & CompanyID & ")".

谢谢。

2 个答案:

答案 0 :(得分:2)

考虑使用中间保存的查询,其中包含您在features = ['x_1','x_2'] label ='y' df = xor_data() x_1 = tf.contrib.layers.real_valued_column("x_1") x_2 = tf.contrib.layers.real_valued_column("x_2") model = tf.contrib.learn.DNNClassifier(hidden_units=[2,2 ], feature_columns=[x_1, x_2], n_classes=3) feature_cols = [tf.contrib.layers.real_valued_column(k) for k in features] def input_fn(data_set): feature_cols = {k: tf.constant(data_set[k].values) for k in features} labels = tf.constant(data_set[label].values) return feature_cols, labels model.fit(input_fn=lambda: input_fn(df), steps=50) 中使用 Company 表的所有INNER JOIN表,方法是运行FULL OUTER JOIN和{{的联合1}}。

因为,MS Access SQL在括号内的LEFT JOIN子句中包含表对,并且其中一些可以是嵌套连接(见下文),所以建议使用Access'来构建查询。查询设计器,如果使用MSAccess.exe GUI程序。另外,请考虑使用表别名来简化语法。

内部查询

RIGHT JOIN

Inner Query Diagram

外部查询 (此处只显示一对表格不需要括号)

JOIN

Outer Query Diagram

请注意:MS Access中的SELECT m.Purchase_Date, d.Quantity, d.Rate, p.Product_Name, c.Product_Category_Name, s.Supplier_Name, s.Supplier_Address, s.Supplier_Phone_No, m.Company_ID, d.Amount, d.Discount, m.TotalAmount, m.Service_Tax, d.TaxableAmount, d.Purchase_Main_ID, d.VAT, m.Purchase_Other_Charges FROM (((PURCHASE_MAIN m INNER JOIN PURCHASE_DETAIL d ON m.Purchase_Main_ID = d.Purchase_Main_ID) INNER JOIN PRODUCT p ON d.Product_ID = p.Product_ID) INNER JOIN PRODUCT_CATEGORY c ON p.Product_Category_ID = c.Product_Category_ID) INNER JOIN SUPPLIER s ON m.Supplier_ID = s.Supplier_ID; 查询没有图表表示。以上两组是单独运行的SELECT q.Purchase_Date, q.Quantity, q.Rate, q.Product_Name, q.Product_Category_Name, q.Supplier_Name, q.Supplier_Address, q.Supplier_Phone_No, c.Company_Name, q.Amount, q.Discount, q.TotalAmount, q.Service_Tax, q.TaxableAmount, q.Purchase_Main_ID, q.VAT, q.Purchase_Other_Charges FROM PRODUCT_INNERQ AS q LEFT JOIN COMPANY AS c ON q.Company_ID = c.Company_ID UNION ALL SELECT q.Purchase_Date, q.Quantity, q.Rate, q.Product_Name, q.Product_Category_Name, q.Supplier_Name, q.Supplier_Address, q.Supplier_Phone_No, c.Company_Name, q.Amount, q.Discount, q.TotalAmount, q.Service_Tax, q.TaxableAmount, q.Purchase_Main_ID, q.VAT, q.Purchase_Other_Charges FROM PRODUCT_INNERQ AS q RIGHT JOIN COMPANY AS c ON q.Company_ID = c.Company_ID WHERE (((q.Company_ID) IS Null)); 语句。注意箭头方向。

答案 1 :(得分:0)

审核FullOuterJoin in Access但是,由于涉及的表数量,您的要求似乎要复杂得多。或许需要打破这一点。查询Purchase,Product和Supplier表。然后在使用Company表的LEFT和RIGHT查询的UNION中使用该查询。