Access SQL视图中的语法错误

时间:2016-06-29 14:52:29

标签: sql ms-access syntax

我总是遇到Access SQL问题。语法要求随机添加括号或移动与SQL Server不同的内容。有谁知道这里的语法错误是什么?该错误集中在FROM子句周围。我知道其他条款还可以,但我把它们留给了参考。

更新:简化了更多代码。我主要关注3 FROM条款。

SELECT 

  ...

FROM 

  (((maintable

    INNER JOIN othertable ON (maintable.A = othertable.A) AND (maintable.B = othertable.B))

    INNER JOIN 

        (SELECT

          ...

        FROM

          (thirdtable)
          INNER JOIN fourthtable ON thirdtable.blah = fourthtable.blah

        WHERE

          fourthtable.something <> 1

        GROUP BY

          ...

        )  AS innerselect1 ON (othertable.item = innerselect1.item) AND (othertable.whse = innerselect1.whse)) AS table1)

    JOIN (

            SELECT ...
            FROM (othertable INNER JOIN maintable ON othertable.item = maintable.item)
            GROUP BY ...
            HAVING ...

          ) AS table2 ON table1.item = table2.item

WHERE 

    ... 

GROUP BY 

  ...

HAVING

  ...

UNION ALL

SELECT 

  ...

FROM 

  (((maintable

    INNER JOIN othertable ON (maintable.A = othertable.A) AND (maintable.B = othertable.B))

    INNER JOIN 

        (SELECT

          ...

        FROM

          (thirdtable)
          INNER JOIN fourthtable ON thirdtable.blah = fourthtable.blah

        WHERE

          fourthtable.something <> 1

        GROUP BY

          ...

        )  AS innerselect1 ON (othertable.item = innerselect1.item) AND (othertable.whse = innerselect1.whse)) AS table1)

    LEFT JOIN (

            SELECT ...
            FROM (othertable INNER JOIN maintable ON othertable.item = maintable.item)
            GROUP BY ...
            HAVING ...

          ) AS table2 ON table1.item = table2.item

WHERE 

    table2.item IS NULL 

GROUP BY 

  ...

HAVING

  ...

UNION ALL

SELECT 

  ...

FROM 

  (((maintable

    INNER JOIN othertable ON (maintable.A = othertable.A) AND (maintable.B = othertable.B))

    INNER JOIN 

        (SELECT

          ...

        FROM

          (thirdtable)
          INNER JOIN fourthtable ON thirdtable.blah = fourthtable.blah

        WHERE

          fourthtable.something <> 1

        GROUP BY

          ...

        )  AS innerselect1 ON (othertable.item = innerselect1.item) AND (othertable.whse = innerselect1.whse)) AS table1)

    RIGHT JOIN (

            SELECT ...
            FROM (othertable INNER JOIN maintable ON othertable.item = maintable.item)
            GROUP BY ...
            HAVING ...

          ) AS table2 ON table1.item = table2.item

WHERE 

    table1.item IS NULL  

GROUP BY 

  ...

HAVING

  ...
;

2 个答案:

答案 0 :(得分:2)

  

[Access SQL]语法需要随机添加括号

它不是随机的。 (我假设你滥用“#34;随机&#34;这个词,就像人们滥用单词&#34;字面意思&#34;。”

当涉及多个JOIN条件时,Access SQL通常需要括号。也就是说,它倾向于抱怨像这样的结构:

SELECT ...
FROM 
    tbl1 
    INNER JOIN tbl2 ON tbl1.fld = tbl2.fld1
    INNER JOIN tbl3 ON tbl2.fld = tbl3.fld2

相反,它需要围绕其中一个(完整的)INNER JOIN的括号。当我手动编写Access SQL时,我更喜欢使用像这样的缩进

SELECT ...
FROM 
    (
        tbl1 
        INNER JOIN 
        tbl2 
        ON tbl1.fld = tbl2.fld1
    )
    INNER JOIN 
    tbl3 
    ON tbl2.fld = tbl3.fld2

第二个例子:

而不是

SELECT ...
FROM 
    tbl1 
    INNER JOIN tbl2 ON tbl1.fld = tbl2.fld1
    INNER JOIN tbl3 ON tbl2.fld = tbl3.fld2
    INNER JOIN tbl4 ON tbl3.fld = tbl4.fld3

更像是

SELECT ...
FROM 
    (
        (
            tbl1 
            INNER JOIN 
            tbl2 
            ON tbl1.fld = tbl2.fld1
        )
        INNER JOIN 
        tbl3 
        ON tbl2.fld = tbl3.fld2
    )
    INNER JOIN 
    tbl4 
    ON tbl3.fld = tbl4.fld3

答案 1 :(得分:1)