0

我在 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; 
4

1 回答 1

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 列,但显然其中一个在其中一个查询中变成了某种文本字段。

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

谢谢

于 2012-07-30T00:24:50.820 回答