我有四個表:加入兩個查詢在Access
客戶
ID | CLIENT_NUM | Year
農場
ID | ClientID | FARM_NUM
場
ID | FarmID | FIELD_NUM | RANK
SoilSheet
ID | FarmID | FieldID | SAMP_NUM | Year
我需要從Rank,FIELD_NUM和Year排序的Soilsheet中獲取數據。所以,我試過這個:
SELECT FL.Rank,FL.FIELD_NUM, S.Year, SAMP_NUM
FROM (((SoilSheet S
INNER JOIN Farms F ON F.ID = S.FarmID)
INNER JOIN Clients C ON C.ID = F.ClientID)
INNER JOIN Fields FL ON FL.ID = S.FieldID)
WHERE C.CLIENT_NUM = 1 AND F.FARM_NUM = 1
ORDER BY C.CLIENT_NUM, F.FARM_NUM, FL.Rank, FL.FIELD_NUM, S.Year
問題是RANK可能會在每年不同,所以它沒有正確排序。例如,我需要按2015年的RANK排序。
通過上述,我得到這樣的結果:
RANK | FIELD_NUM | Year | SAMP_NUM
0 | 19-2 | 2015 | 3000
2 | 20-1 | 2015 | 3001
2 | 20-1 | 2014 | 2002
2 | 20-1 | 2015 | 1003
20 | 19-2 | 2014 | 2001
19-2是RANK 0在2015年,但20在2014年因此,我嘗試這樣做:
SELECT FL.Rank,FL.FIELD_NUM, S.Year, SAMP_NUM
FROM ((Clients C
INNER JOIN Farms F ON F.ClientID = C.ID)
INNER JOIN Fields FL ON FL.FarmID = F.ID)
LEFT JOIN (((SoilSheet S
INNER JOIN Fields FLS ON FLS.ID = S.FieldID)
INNER JOIN Farms FS ON FS.ID = S.FarmID)
INNER JOIN Clients CS ON CS.ID = FS.ClientID)
ON
(FLS.FIELD_NUM = FL.FIELD_NUM
AND FS.FARM_NUM = F.FARM_NUM
AND CS.CLIENT_NUM = C.CLIENT_NUM)
WHERE C.CLIENT_NUM = 1 AND F.FARM_NUM = 1 AND C.Year = 2015
ORDER BY C.CLIENT_NUM, F.FARM_NUM, FL.Rank, FL.FIELD_NUM, S.Year
這工作正常SQL Server:
RANK | FIELD_NUM | Year | SAMP_NUM
0 | 19-2 | 2015 | 3000
0 | 19-2 | 2014 | 2001
2 | 20-1 | 2015 | 3001
2 | 20-1 | 2014 | 2002
2 | 20-1 | 2015 | 1003
在Access中,它給出'Join expression not supported。'。我猜這是因爲Access不喜歡加入INNER連接,但我只是猜測。
任何想法?訂購或另一種方式來加入另一種方式?
我建議你閱讀本:https://msdn.microsoft.com/ en-us/library/bb243855%28v = office.12%29.aspx – 2015-04-02 21:04:42
從那篇文章中,「OUTER JOINs可以嵌套在多表連接中的INNER JOIN中,但INNER JOINs不能嵌套在OUTER JOIN中。」暗示我在左連接中不能有任何內連接,所以現在呢? – 2015-04-02 22:29:05
我建議你通過表格連接表來找出問題何時開始。 MS Access數據庫引擎喜歡括號。在第一次查看時,查詢中括號的數量太小。 – 2015-04-03 05:39:45