NESTED INNER JOIN using MS Access 2003 via ODBC

若如初见. 提交于 2019-12-11 15:19:08

问题


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....


回答1:


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. 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. Maybe you should just try dropping the aliases altogether.




回答2:


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). When these are corrected, your SQL is valid SQL-92 syntax.

Sadly, the problem is that Access (ACE, Jet, whatever) does not support the SQL-92 Standard. Access insists that each nested JOIN clause is put in parentheses.

[Aside: JOINs in parentheses are allowed in Standard SQL because it can potentially change the query results. However Access, does not respect the order specified by the coder and allows itself to evaluate JOINs in order it sees fir. So not only Access's syntax non-compliant with the Standard, there is also a loss of functionality! However, this further problem with Access will have no ill effect for this particular query.]

You have two JOINs in the same scope here:

...
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; because all your JOINs are INNER flavour, it probably doesn't matter where they go.

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.

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;


来源:https://stackoverflow.com/questions/5640692/nested-inner-join-using-ms-access-2003-via-odbc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!