简体   繁体   English

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

[英]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.

相关问题 SQL Server:联接两个表,其中左表包含其每个标识符中的右表的所有行 - SQL Server: join two tables where left table contains all rows of right table in each of its identifier 你如何连接表,以便右表值取决于左表中的两个不同行 - How do you join tables so that right table values depend on two different rows in left table 在SQL连接操作中,如何从左侧联接中获取行,以及如何从右侧表中获取两列的汇总 - In an SQL join operation, how to get the rows from the left join and only the aggregate of two columns from the right table SELECT 来自两个表的记录,其中行可能不存在于第二个表中(与 LEFT JOIN 相关) - SELECT records from two tables where rows might not present in the second table (related to LEFT JOIN) 使用左外连接连接两个表并根据右表列填充新列值 - Joining two tables using left outer join and population a new column values based on the right table column 左联接表,仅使用左表中的一行 - left join tables, use only one row from left table LEFT JOIN两个表 - LEFT JOIN of two tables 在两个桌子上左联接 - LEFT JOIN on two tables SQL-联接两个表,但仅通过左表的出现次数获得联接列的平均值 - SQL - join two tables, but get average of the joined column only by left table occurrences 尝试在两个表上进行左联接,但只希望右表中的一个 - Trying to do a LEFT JOIN on two tables but want only one from the RIGHT TABLE
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM