需要帮助在 MS Access 中实施完全外部联接

Posted

技术标签:

【中文标题】需要帮助在 MS Access 中实施完全外部联接【英文标题】:Need help implementing a Full Outer Join in MS Access 【发布时间】:2014-12-18 04:00:36 【问题描述】:

我无法让查询在 Access 中正常工作。我需要对 dbo_cardpurchases 和 dbo_vendors 进行完全外部联接,以便所有供应商都将出现在查询中,无论是否在该供应商处进行了购买。但 Access 不支持完全外部联接。我还能怎么做?

SELECT dbo_vendors.name, 
       Iif([fundingsourceid] = 10, [amount], "")        AS Credit, 
       Iif(( [fundingsourceid] = 2 ) 
            OR ( [fundingsourceid] = 3 ), [amount], "") AS EBT, 
       Iif([fundingsourceid] = 4, [amount], "")         AS [Match], 
       dbo_cardpurchases.updateddate, 
       dbo_markets.marketid 
FROM   (((dbo_cardpurchases 
          LEFT JOIN dbo_vendors 
                 ON dbo_cardpurchases.vendorid = dbo_vendors.vendorid) 
         LEFT JOIN dbo_cardfundings 
                ON dbo_cardpurchases.cardfundingid = 
                   dbo_cardfundings.cardfundingid) 
        INNER JOIN dbo_marketevents 
                ON dbo_cardpurchases.marketeventid = 
                   dbo_marketevents.marketeventid) 
       INNER JOIN dbo_markets 
               ON dbo_marketevents.marketid = dbo_markets.marketid 
ORDER  BY dbo_vendors.name; 

【问题讨论】:

只是为了确认一下:这些表是连接到 SQL Server 表的 Access 链接表,对吗? 是的,你是对的 【参考方案1】:

正如关于连接here 的***文章中提到的,用于示例表

[员工]

LastName    DepartmentID
----------  ------------
Heisenberg            33
Jones                 33
Rafferty              31
Robinson              34
Smith                 34
Williams            NULL

和[部门]

DepartmentID  DepartmentName
------------  --------------
          31  Sales         
          33  Engineering   
          34  Clerical      
          35  Marketing     

完整的外部连接

SELECT *
FROM employee FULL OUTER JOIN department
    ON employee.DepartmentID = department.DepartmentID;

可以使用三个 SELECT 语句的 UNION ALL 来模拟。所以,在 Access 中你可以这样做

SELECT dbo_employee.LastName, dbo_employee.DepartmentID,
       dbo_department.DepartmentName, dbo_department.DepartmentID
FROM dbo_employee
INNER JOIN dbo_department ON dbo_employee.DepartmentID = dbo_department.DepartmentID

UNION ALL

SELECT dbo_employee.LastName, dbo_employee.DepartmentID,
       NULL, NULL
FROM dbo_employee
WHERE NOT EXISTS (
    SELECT * FROM dbo_department
             WHERE dbo_employee.DepartmentID = dbo_department.DepartmentID)

UNION ALL

SELECT NULL, NULL,
       dbo_department.DepartmentName, dbo_department.DepartmentID
FROM dbo_department
WHERE NOT EXISTS (
    SELECT * FROM dbo_employee
             WHERE dbo_employee.DepartmentID = dbo_department.DepartmentID)

但是,由于您在 SQL Server 中使用链接表,因此您可以只使用 Access 传递查询并使用 T-SQL 执行“真正的”FULL OUTER JOIN:

传递查询总是生成不可更新的记录集,但是针对使用 UNION ALL 的链接表的本机 Access 查询将生成无论如何都不可更新的记录集,所以为什么不利用只是使用 SQL Server 运行查询?

【讨论】:

这让事情变得简单 10 倍! (通过)谢谢。令我惊讶的是,外部连接并没有达到我想要的效果。

以上是关于需要帮助在 MS Access 中实施完全外部联接的主要内容,如果未能解决你的问题,请参考以下文章

EXTJS ||在 GridPanel 中实施复​​选框选择模型时需要帮助

MS Access 2013 中的嵌套 SQL 联接

无法加入备注、OLE 或超链接对象 - Access 2007 - 外部联接

用于 SQL 内部联接的 MS Access 中的括号问题

如何在greenplum中实施CDC?

与 MS SQL Server 完全联接