![](/img/trans.png)
[英]SQL Server: join two tables where left table contains all rows of right table in each of its identifier
[英]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.