Microsoft Access - 基于另一个查询的结果创建查询

时间:2012-09-30 15:51:53

标签: ms-access

此查询生成一个客户ID的表格:

SELECT Transactions.customerId
FROM Transactions 
WHERE Transactions.purchaseDate <#9/1/2012#
GROUP BY Transactions.customerId
HAVING  Sum(Transactions.amountPaid)<>0;

此查询生成一个事务表:

SELECT Customers.firstName, Customers.lastName, Transactions.transactionId, Transactions.product, Transactions.purchaseDate, Transactions.amountPaid, Customers.customerId
FROM Customers INNER JOIN Transactions ON Customers.customerId = Transactions.customerId
WHERE (((Transactions.refundTransaction)=False));

我想仅从第一个查询生成的用户中选择所有事务(第二个查询)。

1 个答案:

答案 0 :(得分:2)

使用MS Access,可以保存这两个查询,然后按查询名称将它们添加到查询设计窗口。添加后,您可以通过将相关字段名称从一个查询拖放到另一个查询来在两个查询之间创建INNER JOIN。如果您可以继续,下一阶段是将查询名称替换为派生表,即查询的sql内容。

例如:

查询1

SELECT ID, CustName FROM Table1 WHERE SomeField > 2

QUERY2

SELECT CustID, TranName FROM Table2 WHERE SomeField > Date()

QUERY3

SELECT * FROM Query1 INNER JOIN Query2 ON Query1.ID = Query2.CustID

代:

SELECT * FROM (
   SELECT ID, CustName 
   FROM Table1 
   WHERE SomeField > 2) As Q1
INNER JOIN (
   SELECT CustID, TranName 
   FROM Table2 WHERE SomeField > Date()) As Q2
ON Q1.ID = Q2.CustID