【问题标题】:Syntax error on Join but no Join in queryJoin 上的语法错误,但查询中没有 Join
【发布时间】:2013-01-17 14:56:18
【问题描述】:

我正在请求获取 3 个不同表中的行数。

所以我在 3 个简单请求之间使用联合来计算每个表中的行数。

但我得到一个错误:“连接操作中的语法错误”

即使我没有加入我的查询...

谁能帮帮我?

这是请求:

Select Sum(asd) as 'totalRows' 
FROM ((Select Count(*) as 'asd' from Machines) 
Union (Select Count(*) as 'asd' from Factures) 
Union (Select Count(*) as 'asd'  From Consommation)) 
 as 'tab1'

【问题讨论】:

  • 我已经通过更改请求解决了这个问题,现在是:Select (Select Count() from Machines) as cntMachines, (Select Count() From Factures) as cntFactures , (Select Count(*) from Consommation) as cntConsommation From [AnyTable]

标签: ms-access join count syntax-error union


【解决方案1】:

取决于您真正想要的...一行多列,或多行具有各自的计数。您的原始查询不正确,因为 UNION 语句应该是其 OWN 查询,以与第一个查询相同的记录格式返回其自己的结果集(即:相同的数字、列名和数据类型。不只使用计数进行采样,采用以下示例语法。

select
      a.NumberField1,
      a.CharField1,
      a.DateField1
   from
      SomeTable a
   where
      a.SomeCondition = 1
UNION
      b.SomeField AS NumberField1,
      b.AnotherField AS CharField1,
      c.SomeDate AS DateField1
   from
      AnotherTable b
   where
      b.TestCondition = 6

上面将返回“SomeTable”中的所有行及其条件,并根据其条件包含“AnotherTable”中的行。如果有任何重复项,则将删除重复项……除非您执行了“UNION ALL”。但请注意,联合本身就是一个 select 语句。

现在,回到你的。不确定它是否/为什么由于被包裹在(parens)中而失败,但会被尝试为

Select 
      Sum(asd) as 'totalRows' 
   FROM 
   (   Select Count(*) as 'asd' 
          from Machines
       UNION ALL
       Select Count(*) as 'asd' 
          from Factures
       UNION ALL
       Select Count(*) as 'asd'  
          From Consommation ) as 'tab1'

我会更改为 union all,因为...说您的 Machines 计数和 Factures 计数均为 175...只有一个原始条目会被退回,而您会为知道计数而挠头不对...如果巧合地所有 3 个来源的计数都相同,请重试 175...您只会返回单个 175 记录,并且比预期的还要远。

【讨论】:

  • 大声笑,“请求太复杂”错误 xD 而我真正想要的是 1 个字段,1 行,这是 3 个不同表之间计数的总和。因此,据我了解,您的第一个建议不会给我想要的东西。但是,您的第二个建议似乎没问题,除了“太复杂”的事情 >.>...谢谢。
【解决方案2】:

我已经通过更改请求解决了这个问题,现在是:

Select (Select Count() from Machines) as cntMachines, 
    (Select Count() From Factures) as cntFactures, 
    (Select Count(*) from Consommation) as cntConsommation 
From [Machines Or any other table]

这不是我的问题的答案,而是一个走动。

仍将不胜感激。 :)

【讨论】:

  • 我之所以选择我的答案,而不是 DRapp 的答案,只是因为我的答案与 DRapp 的答案不同。
猜你喜欢
  • 2013-02-13
  • 1970-01-01
  • 2016-04-10
  • 2012-07-29
  • 2015-07-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-07-14
相关资源
最近更新 更多