我認爲你可以實現這已經使用FULL JOIN
,雖然我對您的架構的具體運作相當朦朧,沿此線服用點應該工作:
SELECT COALESCE(ModTab.ItemID, MedTab.ItemID) AS ItemID,
COALESCE(ModTab.TrfCode, MedTab.TrfCode) AS TrfCode,
COALESCE(ModTab.Component, MedTab.Component) AS Component,
COALESCE(ModTab.StateCode, MedTab.StateCode) AS StateCode,
COALESCE(ModTab.OptCode, MedTab.OptCode) AS OptCode,
CASE WHEN ModTab.OptCode IS NULL THEN 'MedTab Only'
WHEN MedTab.OptCode IS NULL THEN 'ModTab Only'
ELSE 'Both Tables'
END AS MatchStatus
FROM ( SELECT l1.ItemID,
l1.TrfCode,
l2.Component,
l2.StateCode,
l2.OptCode
FROM ModTab m
INNER JOIN Lookup1 l1
ON l1.ItemID = m.ItemID
INNER JOIN Lookup2 l2
ON l2.Component = m.Component
) ModTab
FULL JOIN
( SELECT l1.ItemID,
l1.TrfCode,
l2.Component,
l2.StateCode,
l2.OptCode
FROM MedTab m
INNER JOIN Lookup1 l1
ON l1.TrfCode = m.TrfCode
INNER JOIN Lookup2 l2
ON l2.StateCode = m.StateCode
AND l2.OptCode = m.OptCode
) MedTab
ON ModTab.ItemID = MedTab.ItemID
AND ModTab.TrfCode = MedTab.TrfCode
AND ModTab.Component = MedTab.Component
AND ModTab.StateCode = MedTab.StateCode
AND ModTab.OptCode = MedTab.OptCode;
一些聯接可能需要修正,但原則是那裏,基本上完全加入數據集,這將返回來自每個記錄的所有記錄,其中一個爲null,您知道記錄不在該集合中,其中兩個都不爲null,您知道記錄在兩個記錄中。
編輯
MS-Access不支持FULL JOIN
並具有多種不同的JOIN語法聯接,所以你將不得不使用UNION合併的記錄,然後檢查該紀錄來源。像這樣的東西應該這樣做:
SELECT ItemID,
TrfCode,
Component,
StateCode,
OptCode,
IIF(SUM(MedTab)=0,'ModTab',IIF(SUM(ModTab)=0,'MedTab','Both')) AS TabStatus
FROM ( SELECT Lookup1.ItemID,
Lookup1.TrfCode,
Lookup2.Component,
Lookup2.StateCode,
Lookup2.OptCode,
1 AS MedTab,
0 AS ModTab
FROM ( MedTab
INNER JOIN Lookup1
ON Lookup1.TrfCode = MedTab.TrfCode
)
INNER JOIN Lookup2
ON Lookup2.StateCode = MedTab.StateCode
AND Lookup2.OptCode = MedTab.OptCode
UNION ALL
SELECT Lookup1.ItemID,
Lookup1.TrfCode,
Lookup2.Component,
Lookup2.StateCode,
Lookup2.OptCode,
0 AS MedTab,
1 AS ModTab
FROM ( ModTab
INNER JOIN Lookup2
ON ModTab.Component = Lookup2.Component
)
INNER JOIN Lookup1
ON ModTab.ItemID = Lookup1.ItemID
)
GROUP BY ItemID, TrfCode, Component, StateCode, OptCode;
我沒有測試過這一點,它是一段時間,因爲我沒有任何疑問的訪問,因此手指交叉它的作品第一次!
通常你需要一個內部連接來檢索匹配記錄,外部連接來獲得「不匹配」記錄。 – spiritwalker