Access SQL 视图中的语法错误

Posted

技术标签:

【中文标题】Access SQL 视图中的语法错误【英文标题】:Syntax error in Access SQL View 【发布时间】:2016-06-29 14:52:29 【问题描述】:

我总是在使用 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

  ...
;

【问题讨论】:

Access 当时只能连接两个表。你需要很多括号。 有趣,我不知道。 【参考方案1】:

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

这不是随机的。 (我假设您滥用“随机”这个词的方式与人们滥用“字面意思”这个词的方式大致相同。)

当涉及多个 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

【讨论】:

如果我有 2 个 INNER JOIN 和一个 LEFT JOIN 怎么办? 然后你只需在你想首先执行的 JOIN 周围放一对括号,结果将被 JOIN 到第三个表中。 我有点困惑。你的意思是FROM (tbl1 INNER JOIN tbl2 ON ... INNER JOIN tbl3) ON ... LEFT JOIN tbl4 ON ... 还是FROM ((tbl1 INNER JOIN tbl2 ON ...) INNER JOIN tbl3) ON ... LEFT JOIN tbl4 ON ... 我用更简单的代码更新了我的问题,以便更好地显示我当前使用的括号。 对不起,我误读了您的原始评论。我会尽快更新我的答案。【参考方案2】:

Access SQL 不支持FULL OUTER JOIN

有关解决方法,请参阅例如How do I write a full outer join query in access

或https://***.com/search?tab=relevance&q=%5bms-access%5d%20FULL%20OUTER%20JOIN:p

【讨论】:

是否存在相当于在一个字段上连接 2 个表并返回字段不匹配的行的 Access?

以上是关于Access SQL 视图中的语法错误的主要内容,如果未能解决你的问题,请参考以下文章

Access 2010 中的 INSERT INTO 语句中出现 SQL 语法错误

MS Access SQL 语法错误

Access 2010:查询表达式中的语法错误(缺少运算符)

odbc_exec():SQL 错误:[Microsoft][ODBC Microsoft Access Driver] 查询表达式中的语法错误(缺少运算符)

JSP向ACCESS插入记录的报SQL语法错误

使用 Access 和 pyodbc 的 INSERT 语句中的语法错误