繁体   English   中英

左联接两个表,然后仅将新行追加到表

[英]Left join two tables, and then append only new rows to table

我试图从tblForecast中获取所有记录,并从tblOpenJobs中获取匹配记录,并将它们追加到tblWorkingTable中,但前提是[tbWorkingTable中尚不存在[Job#]。

第一部分(通过第一个左连接)工作正常,但是第二个左连接和Where语句失败,出现语法错误:

查询表达式'A. [Job#] = B. [Job#] LEFT JOIN tblWorkingTable AS C ON A. [Job#] = C. [Job#“中的语法错误(缺少运算符)。

我对SQL还是很陌生,所以我不确定我哪里出错了。 我可能可以使它与两个独立的查询一起工作,但是将其全部工作于一个中确实是理想的。

INSERT INTO tblWorkingTable ( [Rec'd], ForecastMonth, [Ship Week], [Commit Date], [Job #], Customer, [Part #], Released, [Forecast Qty], [Actual Qty], Shipped, [Sales Price], [Sales Value], Invoice, Comments )
SELECT B.[Rec'd], A.ForecastMonth, A.[Ship Week], A.[Commit Date], A.[Job #], A.Customer, A.[Part #], B.Released, A.Qty AS [Forecast Qty], B.Qty AS [Actual Qty], B.Shipped, A.[Sales Price], A.[Sales Value], A.Invoice, A.Comments
FROM tblForecast AS A 
LEFT JOIN tblOpenJobs AS B ON A.[Job #] = B.[Job #]
LEFT JOIN tblWorkingTable AS C ON A.[Job #] = C.[Job #]
Where ((C.[Job #]) is Null);

是的,我知道,这些字段名称中不应包含特殊字符。 在数据导入期间,我需要为这些名称分配其他名称。

MS Access需要在连接周围加上括号。 尝试这个:

INSERT INTO tblWorkingTable ( [Rec'd], ForecastMonth, [Ship Week], [Commit Date], [Job #], Customer, [Part #], Released, [Forecast Qty], [Actual Qty], Shipped, [Sales Price], [Sales Value], Invoice, Comments )
    SELECT B.[Rec'd], A.ForecastMonth, A.[Ship Week], A.[Commit Date], A.[Job #], A.Customer, A.[Part #], B.Released, A.Qty AS [Forecast Qty],
           B.Qty AS [Actual Qty], B.Shipped, A.[Sales Price], A.[Sales Value], A.Invoice, A.Comments
    FROM (tblForecast AS A LEFT JOIN
          tblOpenJobs AS B
          ON A.[Job #] = B.[Job #] 
         ) LEFT JOIN
         tblWorkingTable AS C
         ON A.[Job #] = C.[Job #]
    WHERE C.[Job #] is Null;

暂无
暂无

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

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