简体   繁体   English

通过ODBC使用MS Access 2003进行嵌套的内部联接

[英]NESTED INNER JOIN using MS Access 2003 via ODBC

If this works: 如果可行:

SELECT COUNT(t1.ID) AS count FROM Project t1
INNER JOIN (SELECT DISTINCT t.Site,t.id FROM _Equipment_id t WHERE t.OEM LIKE '%ABC%') t2 ON t1.Site=t2.Site AND t1.id=t2.id

and this works: 这有效:

SELECT COUNT(t3.ID) AS count FROM Wall t3
INNER JOIN Project t1 ON t3.Project_number=t1.Project_number

Why doesn't this work: 为什么这样不起作用:

SELECT COUNT(t3.ID) AS count FROM Wall t3
INNER JOIN Project t1 ON t3.Project_number=t1.Project_number
INNER JOIN (SELECT DISTINCT t.Site,t.id FROM _Equipment_id t WHERE t.OEM LIKE '%ABC%') t2 ON t1.Site=t2.Site AND t1.id=t2.id

Ultimately, I have 10 tables like the Wall table that I am trying to get a total count from the first SELECT.... 最终,我有10个表,例如Wall表,我正尝试从第一个SELECT中获取总数。

SELECT COUNT(t3.ID) AS count FROM Wall t3
INNER JOIN (Project t1 
INNER JOIN (SELECT DISTINCT t.Site,t.id FROM _Equipment_id t WHERE t.OEM LIKE '%ABC%') t2 
ON t1.Site=t2.Site AND t1.id=t2.id)
ON t3.Project_number=t1.Project_number

Maybe it's just a syntax error? 也许这只是语法错误? Office Help at the bottom where they mention nesting. 他们在底部提到嵌套的Office帮助 The other possibility is that the aliases are somehow scoped so that they are not available to the join, but I'm no expert on MS Access. 另一种可能性是,别名在某种程度上受到限制,因此它们对联接不可用,但是我不是MS Access方面的专家。 Maybe you should just try dropping the aliases altogether. 也许您应该尝试完全删除别名。

You have a couple of minor issues with your code: a table name that starts with an underscore character ( _Equipment_id ) and an AS clause ("alias") that is a SQL keyword ( AS count ). 您的代码有两个小问题:以下划线字符( _Equipment_id )开头的表名和作为SQL关键字( AS count )的AS子句(“ alias”)。 When these are corrected, your SQL is valid SQL-92 syntax. 更正这些错误后,您的SQL即为有效的SQL-92语法。

Sadly, the problem is that Access (ACE, Jet, whatever) does not support the SQL-92 Standard. 可悲的是,问题在于Access(ACE,Jet等)不支持SQL-92标准。 Access insists that each nested JOIN clause is put in parentheses. Access坚持将每个嵌套的JOIN子句放在括号中。

[Aside: JOIN s in parentheses are allowed in Standard SQL because it can potentially change the query results. [此外:在标准SQL中允许使用括号中的JOIN ,因为它可能会更改查询结果。 However Access, does not respect the order specified by the coder and allows itself to evaluate JOIN s in order it sees fir. 但是,Access不遵守编码器指定的顺序,而是允许自己评估JOIN的顺序,以使其符合fir的要求。 So not only Access's syntax non-compliant with the Standard, there is also a loss of functionality! 因此,不仅Access的语法与标准不兼容,而且还会失去功能! However, this further problem with Access will have no ill effect for this particular query.] 但是,Access的此进一步问题不会对该特定查询产生不良影响。]

You have two JOIN s in the same scope here: 您在同一范围内有两个JOIN

...
INNER JOIN Project t1 ON t3.Project_number=t1.Project_number
INNER JOIN
...

Your code needs to work around Access's problem by enclosing the JOIN in parentheses; 您的代码需要通过将JOIN括在括号中来解决Access的问题。 because all your JOIN s are INNER flavour, it probably doesn't matter where they go. 因为您所有的JOIN都具有INNER风味,所以去哪里可能都没有关系。

Also, as regards correcting your AS clause, Access again doesn't support Standard SQL's quoted identifiers ( ...AS "count"... ) and insists you use its proprietary square brackets syntax ( ...AS [count]... ) -- of course, you could choose a different name but there may exist application code that relies on it. 此外,关于更正AS子句,Access再次不支持Standard SQL的带引号的标识符( ...AS "count"... ),并坚持要求您使用其专有的方括号语法( ...AS [count]... )-当然,您可以选择其他名称,但是可能存在依赖于该名称的应用程序代码。

Code to workaround both Access problems: 解决两个访问问题的代码:

SELECT COUNT(t3.ID) AS [count] 
  FROM (Wall t3
       INNER JOIN Project AS t1 
          ON t3.Project_number = t1.Project_number)
       INNER JOIN (
                   SELECT DISTINCT t.Site,t.id 
                     FROM _Equipment_id AS t 
                    WHERE t.OEM LIKE '%ABC%'
                  ) AS t2 
          ON t1.Site = t2.Site 
             AND t1.id = t2.id;

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

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