[英]Left join two tables, and then append only new rows to table
I'm trying to take all records from tblForecast and the matching records from tblOpenJobs and append them to tblWorkingTable, but only if the [Job #] does not yet exist in tblWorkingTable. 我试图从tblForecast中获取所有记录,并从tblOpenJobs中获取匹配记录,并将它们追加到tblWorkingTable中,但前提是[tbWorkingTable中尚不存在[Job#]。
The first part (through the first Left Join) works fine, but the second left join and Where statement fail with a Syntax error: 第一部分(通过第一个左连接)工作正常,但是第二个左连接和Where语句失败,出现语法错误:
Syntax error (missing operator) in query expression 'A.[Job #]= B.[Job #] LEFT JOIN tblWorkingTable AS C ON A.[Job #] = C.[Job#'.
查询表达式'A. [Job#] = B. [Job#] LEFT JOIN tblWorkingTable AS C ON A. [Job#] = C. [Job#“中的语法错误(缺少运算符)。
I'm pretty new to SQL, so I'm not sure where I'm going wrong. 我对SQL还是很陌生,所以我不确定我哪里出错了。 I could probably get it to work with two separate queries, but it would be really ideal to get it all to work in one.
我可能可以使它与两个独立的查询一起工作,但是将其全部工作于一个中确实是理想的。
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);
And yes, I know, there shouldn't be special characters in those field names. 是的,我知道,这些字段名称中不应包含特殊字符。 I need to assign those a different name during the data import.
在数据导入期间,我需要为这些名称分配其他名称。
MS Access requires parentheses around joins. MS Access需要在连接周围加上括号。 Try this:
尝试这个:
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.