Access SQL - 匹配来自两个查询的记录

时间:2014-12-17 23:27:55

标签: sql ms-access-2013

场合

我有两个查询从各种相关表中选择信息。一个选择year = 2012的所有记录,另一个选择year = 2013

的记录
SELECT c.Company_ID, c.Company_Name, e.Employee_ID, e.Employee_Name, p.Position
FROM ((tbl_Company AS c
INNER JOIN tbl_Employee AS e ON c.Company_ID = e.Company_ID) 
INNER JOIN tbl_Position AS p ON e.Employee_ID = p.Employee_ID) 
INNER JOIN tbl_Report AS r ON (r.Report_ID = p.Report_ID) AND (c.Company_ID = r.Company_ID)
WHERE (((r.Report_Year)=2012))

SELECT c.Company_ID, c.Company_Name, e.Employee_ID, e.Employee_Name, p.Position
FROM ((tbl_Company AS c
INNER JOIN tbl_Employee AS e ON c.Company_ID = e.Company_ID) 
INNER JOIN tbl_Position AS p ON e.Employee_ID = p.Employee_ID) 
INNER JOIN tbl_Report AS r ON (r.Report_ID = p.Report_ID) AND (c.Company_ID = r.Company_ID)
WHERE (((r.Report_Year)=2013))

问题

如何构建查询以返回在两个查询中匹配的Company_IDCompany_NameEmployee_IDEmployee_NamePosition(即存在于2012年和2013年)?


更新

使用子查询我可以选择2012年的company_id,仅限于2013年。我现在如何确保只选择employee_idposition 2013年也和2012年一样吗?

SELECT c.Company_ID, c.Company_Name, e.Employee_ID, e.Employee_Name, p.Position
FROM ((tbl_Company AS c
INNER JOIN tbl_Employee AS e ON c.Company_ID = e.Company_ID) 
INNER JOIN tbl_Position AS p ON e.Employee_ID = p.Employee_ID) 
INNER JOIN tbl_Report AS r ON (r.Report_ID = p.Report_ID) AND (c.Company_ID = r.Company_ID)
WHERE r.Report_Year=2013
AND
c.Company_ID IN
(
    SELECT c.Company_ID
    FROM ((tbl_Company AS c
    INNER JOIN tbl_Employee AS e ON c.Company_ID = e.Company_ID) 
    INNER JOIN tbl_Position AS p ON e.Employee_ID = p.Employee_ID) 
    INNER JOIN tbl_Report AS r ON (r.Report_ID = p.Report_ID) AND (c.Company_ID = r.Company_ID)
    WHERE r.Report_Year=2012
)

3 个答案:

答案 0 :(得分:0)

如果两个查询都返回相同的列,则可以使用UNION ALL子句合并两个查询的结果。

SELECT c.Company_ID, c.Company_Name, e.Employee_ID, e.Employee_Name, p.Position_ID, p.Position
FROM ((tbl_Company AS c
INNER JOIN tbl_Employee AS e ON c.Company_ID = e.Company_ID) 
INNER JOIN tbl_Position AS p ON e.Employee_ID = p.Employee_ID) 
INNER JOIN tbl_Report AS r ON (r.Report_ID = p.Report_ID) AND (c.Company_ID = r.Company_ID)
WHERE (((r.Report_Year)=2012))
UNION ALL
SELECT c.Company_ID, c.Company_Name, e.Employee_ID, e.Employee_Name, p.Position_ID, p.Position
FROM ((tbl_Company AS c
INNER JOIN tbl_Employee AS e ON c.Company_ID = e.Company_ID) 
INNER JOIN tbl_Position AS p ON e.Employee_ID = p.Employee_ID) 
INNER JOIN tbl_Report AS r ON (r.Report_ID = p.Report_ID) AND (c.Company_ID = r.Company_ID)
WHERE (((r.Report_Year)=2013))

http://msdn.microsoft.com/en-us/library/bb208962%28v=office.12%29.aspx

但您也可以使用IN子句使其更简单,更短:

SELECT c.Company_ID, c.Company_Name, e.Employee_ID, e.Employee_Name, p.Position_ID, p.Position
FROM ((tbl_Company AS c
INNER JOIN tbl_Employee AS e ON c.Company_ID = e.Company_ID) 
INNER JOIN tbl_Position AS p ON e.Employee_ID = p.Employee_ID) 
INNER JOIN tbl_Report AS r ON (r.Report_ID = p.Report_ID) AND (c.Company_ID = r.Company_ID)
WHERE (((r.Report_Year) IN (2012, 2013)))

答案 1 :(得分:0)

有交叉,类似于union:http://www.sqlguides.com/sql_intersect.php

  

SQL INTERSECT是一种允许您选择相关信息的查询   从2个表中,这是将2个SELECT语句组合成1并显示   它出来了。

     

INTERSECT产生出现在两个查询中的行。这意味着   INTERSECT命令充当AND运算符(仅当选择值时才选择值)   它出现在两个陈述中。)

如果您使用SQL Fiddle,您可以让人们更轻松地试用您的SQL。我设置了一个非常简单的表格来显示示例:http://sqlfiddle.com/#!6/baf67/3

答案 2 :(得分:0)

这很笨重,可能会有更优雅的方式,但我认为这可以实现你想要的:

SELECT DISTINCT * FROM
(SELECT c.Company_ID, c.Company_Name, e.Employee_ID, e.Employee_Name, p.Position_ID, p.Position
FROM ((tbl_Company AS c
INNER JOIN tbl_Employee AS e ON c.Company_ID = e.Company_ID) 
INNER JOIN tbl_Position AS p ON e.Employee_ID = p.Employee_ID) 
INNER JOIN tbl_Report AS r ON (r.Report_ID = p.Report_ID) AND (c.Company_ID = r.Company_ID)
WHERE (((r.Report_Year)=2012))
)
t1 INNER JOIN

(SELECT c.Company_ID, c.Company_Name, e.Employee_ID, e.Employee_Name, p.Position_ID, p.Position
FROM ((tbl_Company AS c
INNER JOIN tbl_Employee AS e ON c.Company_ID = e.Company_ID) 
INNER JOIN tbl_Position AS p ON e.Employee_ID = p.Employee_ID) 
INNER JOIN tbl_Report AS r ON (r.Report_ID = p.Report_ID) AND (c.Company_ID = r.Company_ID)
WHERE (((r.Report_Year)=2013))
)

t2 ON t1.Company_ID = t2.Company_ID
AND
t1.Employee_ID = t2.Employee_ID
AND t1.Position_ID = t2.Position_ID