【发布时间】:2012-08-21 20:47:17
【问题描述】:
我正在尝试合并两组不同的数据。在两组数据之间,有些条目可能是重复的。
表 1:
Common Field A | Field 1 | Field 2
---------------------------------
a a1 a2
b b1 b2
表 2:
Common Field B | Field 3 | Field 4
---------------------------------
c c1 c2
a a1 a2
我对公共字段进行了联合查询以获取:
Common Field
---------------
a
b
c
现在我做了 2 次连接。联合查询和表 1 之间的一次连接,以及联合查询和表 2 之间的一次连接。基本上每个连接都已完成,以便列出联合查询的所有条目,并且表中的公共字段匹配的行将列出联合查询条目。这样做之后,我创建了一个包含所有列的查询(记住上面的两个连接)。这是我的预期:
Common Field | Common Field A | Common Field B | Field 1 | Field 2 | Field 3 | Field 4
------------------------------------------------------------------------------------------
a a a a1 a2 a1 a2
b b b1 b2
c c c1 c2
这是我得到的:
Common Field | Common Field A | Common Field B | Field 1 | Field 2 | Field 3 | Field 4
------------------------------------------------------------------------------------------
a a a a1 a2 a1 a2
b b #Error b1 b2
c #Error c c1 c2
我不确定为什么我会收到 #Error 标志,我也很困惑为什么我只为公共字段条目获得它们。如果有的话,我希望至少能像这样得到它们:
Common Field | Common Field A | Common Field B | Field 1 | Field 2 | Field 3 | Field 4
------------------------------------------------------------------------------------------
a a a a1 a2 a1 a2
b b #Error b1 b2 #Error #Error
c #Error c #Error #Error c1 c2
有没有办法摆脱它们?我试过使用 IIF(IsError(....)) 但这没有用。我在网上发现了一些尝试,人们试图摆脱 #Error 消息但无法解决它。我知道最好找到问题的根源,但在这一点上,我可以摆脱#Error 消息本身。关于我应该如何处理这个问题的任何想法?
编辑:这是一些示例 SQL:
SELECT qryUnion.CommonField, tbl1.CommonFieldA, tbl2.CommonFieldB, tbl1.Field1, tbl1.Field2, tbl2.Field3, tbl2.Field4
FROM tbl2 RIGHT JOIN ( tbl1 RIGHT JOIN qryUnion ON tbl1.CommonFieldA = qryUnion.CommonField) ON tbl2.CommonFieldB = qryUnion.CommonField;
EDIT2:如果重要的话,这里是连接的 SQL:
SELECT tbl1.CommonFieldA FROM tbl1 UNION tbl2.CommonFieldB FROM tbl2
【问题讨论】:
-
虽然您的逻辑已经发布,请提供您正在使用的 SQL。这是确定正在发生的事情的唯一确定方法。
-
嗨 Remou 和 RobB,感谢您的回复!根据要求,我发布了我的 SQL,希望对您有所帮助。
-
@KryptKeeper:什么版本的 MS Access?我刚刚在 Access 2010 中尝试了您的示例,没有任何
#Error就可以正常工作。如果您的 MS Access 版本不支持,您可以尝试使用FULL OUTER JOIN或 the equivalent。 -
我也刚刚在 MS Access 2003 中尝试过,而且效果也很好(请注意,您的 UNION 查询中有错字:应该是
SELECT tbl1.CommonFieldA as CommonField FROM tbl1 UNION SELECT tbl2.CommonFieldB FROM tbl2;)。你确定你的数据库没有损坏吗?尝试压缩并修复它,看看它是否会消失,或者尝试将您的数据复制到一个新的数据库中。您的人为示例是否可能过于简单而无法代表您看到错误的实际场景? 编辑: 尝试了 Access 97,仍然可以正常工作。 -
我同意这两个 cmets,一旦对 UNION 进行了更改,它就可以工作。