MS Access:FROM子句中的语法错误

时间:2016-05-18 18:37:32

标签: sql join ms-access-2013

我收到指向FROM子句的语法错误,但我无法弄明白。这里的目标是使用子查询在加入之前过滤并返回一些信息。

我尝试过的内容:This question on SO建议我用括号围绕2 LEFT JOINS。我这样做了,但没有帮助。下面是结果,我得到的错误在标题中。

在围绕JOINS进行括号操作之前,可能会或可能不重要,我收到以下错误:Syntax error (missing operator in query expression 'tbl_HersheySAPInventory.item = maxdatejob.item LEFT JOIN (SELECT * FROM dbo_Item_by_Location_LP_All WHERE qty_on_hand >'.

SELECT 

    tbl_HersheySAPInventory.ID, 
    tbl_HersheySAPInventory.item, 
    dbo_job.job_date, 
    dbo_job.job, 
    dbo_job.suffix,
    Sum(comptable.qty_on_hand) AS SumOfqty_on_hand,
    comptable.whse,
    comptable.product_code

FROM 

    tbl_HersheySAPInventory 

    (LEFT JOIN 

        (SELECT 

            dbo_job.job_date, 
            dbo_job.job, 
            dbo_job.suffix, 
            dbo_job.item AS FG,
            dbo_jobmatl.item

        FROM 

            dbo_job 
            INNER JOIN dbo_jobmatl ON dbo_job.job = dbo_jobmatl.job AND dbo_job.suffix = dbo_jobmatl.suffix 

        WHERE

            dbo_job.item Not Like "Indirect" AND
            dbo_job.job Not Like "C0*" AND
            dbo_job.job Not Like "*R0*"

        ) AS maxdatejob 

    ON tbl_HersheySAPInventory.item = maxdatejob.item)

    (LEFT JOIN

        (SELECT * FROM dbo_Item_by_Location_LP_All WHERE qty_on_hand > 0 AND whse = Forms!MainForm!Combo353)  AS comptable

    ON comptable.item = tbl_HersheySAPInventory.item)


ORDER BY 

    tbl_HersheySAPInventory.item, 
    dbo_job.job_date;

1 个答案:

答案 0 :(得分:1)

您的括号可能有问题。我有类似的问题将SQL写入Access - 似乎有一些奇怪的要求包围JOINS和WHERE子句的parens。

我建议尝试的伪代码是:

FROM 
(
(
Table A
)
LEFT JOIN
  (
  Subquery1
  )
)
LEFT JOIN
  (
  Subquery2
  )

我已经尝试了下面的更新。试一试,让我知道:

SELECT 

    tbl_HersheySAPInventory.ID, 
    tbl_HersheySAPInventory.item, 
    dbo_job.job_date, 
    dbo_job.job, 
    dbo_job.suffix,
    comptable.whse,
    comptable.product_code,
    Sum(comptable.qty_on_hand) AS SumOfqty_on_hand    
FROM 
(
(
    tbl_HersheySAPInventory 



    LEFT JOIN 

        (SELECT 

            dbo_job.job_date, 
            dbo_job.job, 
            dbo_job.suffix, 
            dbo_job.item AS FG,
            dbo_jobmatl.item

        FROM 

            dbo_job 
            INNER JOIN dbo_jobmatl ON dbo_job.job = dbo_jobmatl.job AND dbo_job.suffix = dbo_jobmatl.suffix 

        WHERE

            dbo_job.item Not Like "Indirect" AND
            dbo_job.job Not Like "C0*" AND
            dbo_job.job Not Like "*R0*"

        ) AS maxdatejob 

    ON tbl_HersheySAPInventory.item = maxdatejob.item
    )
    )

    LEFT JOIN

        (SELECT * FROM dbo_Item_by_Location_LP_All WHERE qty_on_hand > 0 AND whse = Forms!MainForm!Combo353)  AS comptable

    ON comptable.item = tbl_HersheySAPInventory.item

GROUP BY

    tbl_HersheySAPInventory.ID, 
    tbl_HersheySAPInventory.item, 
    dbo_job.job_date, 
    dbo_job.job, 
    dbo_job.suffix,
    comptable.whse,
    comptable.product_code   


ORDER BY 

    tbl_HersheySAPInventory.item, 
    dbo_job.job_date;