简体   繁体   English

在MS Access中,我从表查询中得到意外结果

[英]In MS Access, I am getting unexpected result from a query of tables

I have the following tables with the following fields: 我有带有以下字段的下表:

  • BeliefT (BeliefID, Beliefs, Topic, Topic2, Topic3, Topic4)
  • ArgumentAgreeT (ReasonToAgreeID, ConclusionA, Reason to agree) The values for these fields, come from lookups to BeliefT.BeliefID and BeliefT.Beliefs . ArgumentAgreeT (ReasonToAgreeID, ConclusionA, Reason to agree)这些字段的值来自对BeliefT.BeliefIDBeliefT.Beliefs查找。
  • ArgumentDisagreeT (ReasonToDisagreeID, ConclusionD, Reason to Disagree) The values for these fields, also come from lookups to BeliefT.BeliefID and BeliefT.Beliefs . ArgumentDisagreeT (ReasonToDisagreeID, ConclusionD, Reason to Disagree)的值也来自对BeliefT.BeliefIDBeliefT.Beliefs查找。

I have tried everything I can think of. 我已经尝试了所有我能想到的。 This SQL statement isn't too bad, but it gives me weird results: 这个SQL语句还不错,但是它给了我奇怪的结果:

SELECT BeliefT.BeliefID, 
       BeliefT.Beliefs, 
       ArgumentsAgreeT.[Reason to agree], 
       ArgumentsDisagreeT.[Reason to Disagree] 
FROM (BeliefT 
      LEFT JOIN ArgumentsAgreeT ON BeliefT.BeliefID = ArgumentsAgreeT.[ConclusionA]) 
LEFT JOIN ArgumentsDisagreeT ON BeliefT.BeliefID = ArgumentsDisagreeT.[ConclusionD];

The above SQL statement includes the the first two things from the first table, and the Reason to agree and Reason to Disagree from the 2nd and 3rd tables. 上面的SQL语句包括第一张表中的前两件事,以及第二张和第三张表中的“ Reason to agree Reason to DisagreeReason to Disagree Reason to agree Reason to Disagree As you can tell from the SQL statement, the join type is an arrow pointing from BeliefT.BeliefID to both ArgumentsAgreeT.ConclusionA and ArgumentsDisagreeT.ConclusionD 从SQL语句可以看出,联接类型是从BeliefT.BeliefID指向ArgumentsAgreeT.ConclusionAArgumentsDisagreeT.ConclusionD的箭头

The problem is that if there is more than one reason to agree, and only one reason to disagree, that it repeats the reason to disagree for every reason to agree . 问题是, 如果有一个以上的同意理由,而只有一个不同意的理由,那么它就会出于各种理由而重复不同意的理由 In other my agree table ( ArgumentAgreeT ) has 5 records. 在其他我的agree表( ArgumentAgreeT )中有5条记录。 My Disagree table only has 2 records. 我的不同意表只有2条记录。 Whenever there is a record from the Agree table, it duplicates the single record from my disagree table. 只要“ Agree表中有一条记录,它就会复制我“不Agree表中的一条记录。

Thanks for any help! 谢谢你的帮助!

First, there are no self-joins here which are multiple references of same table with different aliases in same query. 首先,这里没有自联接 ,它们是同一表的多个引用,并且在同一查询中具有不同的别名。

Second, SQL queries are technically Cartesian products returning the combined set (query) from multiple sets (joined tables) according to a corresponding key. 其次,从技术上讲,SQL查询是笛卡尔积 ,根据相应的键从多个集合(联接表)返回组合的集合(查询)。 So yes, repeat records as they are paired to the other table will appear as particular combination sets. 因此,是的,重复记录与另一张表配对时,将显示为特定的组合集。 With 5 X 2 on the same BeliefID, you would naturally yield at least 10 records for every BeliefID. 在同一个BeliefID上使用5 X 2,您自然会为每个BeliefID至少产生10条记录。

Also, what did you expect the result to be? 另外,您期望结果是什么? The query would not return less than the data that exists. 查询返回的数据将少于现有数据。 Notice too the entire row is not a duplicate (though records repeat within a column) as each returned combination would be distinct. 注意整个行不是重复的(尽管记录在列中重复),因为每个返回的组合都是不同的。 So even using a DISTINCT will not work. 因此,即使使用DISTINCT也不起作用。

Possibly, you want to run a Union query that stacks Agree and Disagree items as side by side would not return less than 10 records with table joins for each BeliefID. 可能需要运行一个并排堆叠“同意”和“不同意”项目的并集查询,对于每个BeliefID,该查询不会以表联接返回不少于10条记录。

SELECT BeliefT.BeliefID, BeliefT.Beliefs, ArgumentsAgreeT.[Reason to agree] As [Reason], 'Agree' As [Type]
FROM BeliefT LEFT JOIN ArgumentsAgreeT ON BeliefT.BeliefID = ArgumentsAgreeT.[ConclusionA]
UNION
SELECT BeliefT.BeliefID, BeliefT.Beliefs,  ArgumentsDisagreeT.[Reason to Disagree] As [Reason], 'Disagree' As Type
FROM BeliefT  LEFT JOIN ArgumentsDisagreeT ON BeliefT.BeliefID = ArgumentsDisagreeT.[ConclusionD];

Finally, do note your issue may not be a query problem but a table design issue. 最后,请注意您的问题可能不是查询问题,而是表设计问题。 Usually when complex queries are required, data perhaps is not normalized or report requests don't follow data storage structure. 通常,当需要复杂的查询时,数据可能无法规范化,或者报告请求不遵循数据存储结构。 The concept of normalization is to group data elements into logical groupings to avoid duplication. 标准化的概念是将数据元素分组为逻辑分组,以避免重复。 Reasons could be its own table with one-to-many link with Beliefs , containing a category/type field to distinguish Agree or Disagree. Reasons可能是其自己的表,并且具有与Beliefs的一对多链接,其中包含用于区分“同意”或“不同意”的类别/类型字段。 Doing so, you wouldn't need a union query like the above. 这样做,您将不需要像上面那样的联合查询。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM