Access 2010 WHERE子句无法按预期工作

时间:2012-07-29 23:34:36

标签: where-clause ms-access-2010

我在Access 2010中有一个复杂的查询,其中: query1是查询WHERE field1<> 8 和 query2是相同的查询WHERE field1 = 8.

然后我在做:

SELECT * FROM query1 
INNER JOIN query2 ON query1.field2=query2.field2 
WHERE query1.field3=query2.field3

这会返回0结果。

但是,当我将其更改为:

SELECT * from query1 
INNER JOIN query2 ON query1.field2=query2.field2 
WHERE query1.field3=5 AND query2.field3=5

我确实得到了结果。这对任何人都有意义吗?它可能与被视为文本字段的字段有关吗?除了它没有引号,所以我不知道为什么会这样。

如果你以前看过这个,请告诉我。

这是完整的查询:

SELECT *
FROM   (SELECT [transactions by category].[categoryid],
           Month([account transactions].[transaction date]) AS TransMonth,
           Year([account transactions].[transaction date])  AS TransYear,
           SUM([transactions by category].[amount])         AS Amount
    FROM   (categories
            INNER JOIN [transactions by category]
                    ON categories.id =
           [transactions by category].categoryid)
           INNER JOIN [account transactions]
                   ON [transactions by category].transactionid =
                      [account transactions].id
    WHERE  [account transactions].[transaction type] <> 8
    GROUP  BY [transactions by category].[categoryid],
              Year([account transactions].[transaction date]),
              Month([account transactions].[transaction date])) AS
   TransactionCredits
   INNER JOIN (SELECT [transactions by category].[categoryid],
                      Month([account transactions].[transaction date]) AS
                                         TransMonth,
                      Year([account transactions].[transaction date])  AS
                      TransYear
                                         ,
                      SUM(
          [transactions by category].[amount])
               FROM   (categories
                       INNER JOIN [transactions by category]
                               ON categories.id =
   [transactions by category].categoryid)
                      INNER JOIN [account transactions]
                              ON [transactions by category].transactionid =
                                 [account transactions].id
               WHERE  [account transactions].[transaction type] = 8
               GROUP  BY [transactions by category].[categoryid],
                         Year([account transactions].[transaction date]),
                         Month([account transactions].[transaction date]))
              AS
                                 TransactionDebits
           ON TransactionCredits.[categoryid] =
              TransactionDebits.[categoryid]
WHERE  TransactionCredits.transyear = TransactionDebits.transyear
   AND TransactionCredits.transmonth = 8
   AND TransactionDebits.transmonth = 8; 

1 个答案:

答案 0 :(得分:0)

所以,我几个月来一直在撞墙。我终于找到了解决方案,但我不完全理解为什么会这样。

而不只是说

 SELECT * 
 FROM query1 
 INNER JOIN query2 ON query1.field2=query2.field2 
 WHERE query1.field3=query2.field3"

我将WHERE子句更改为

VAL(query1.field3)=VAL(query2.field3)

这两个字段最初都是整数ID列,但显然其中一个字段在某个查询中的某处变成了某种文本字段。

我仍然有兴趣听听有关其工作原理的解释。

由于