我尝试在MS Access中执行以下SQL。
基本上TAB3
用作翻译表
SELECT *
FROM TAB1 T1 INNER JOIN TAB2 T2
ON T1.MemNo = T2.MemID
AND (T1.SID = (SELECT x.Col1 FROM TAB3 x WHERE x.Col2 = T2.SVID))
但它给了我一个语法错误
可能是什么问题
更新
TAB1
MemNo SID
116537 S110
116537 D011
575788 D012
214438 S110
434675 D114
214438 D011
208368 D012
208368 S110
TAB2
MemID SVID
116537 110
116537 11
214438 11
434675 114
214438 110
575788 12
208368 12
208368 110
TAB3
Col1 Col2
D011 11
S110 110
D114 114
D012 12
由于
答案 0 :(得分:2)
尝试使用exists:
SELECT *
FROM TAB1 T1 INNER JOIN TAB2 T2
ON T1.MemNo = T2.MemID
WHERE EXISTS (SELECT * FROM TAB3 x WHERE x.Col2 = T2.SVID AND x.Col1 = T1.SID)
答案 1 :(得分:2)
为什么不将子查询移动到查询的WHERE语句?
SELECT *
FROM TAB1 T1
INNER JOIN TAB2 T2 ON T1.MemNo = T2.MemID
WHERE EXISTS ( SELECT 1
FROM TAB3 x
WHERE x.Col2 = T2.SVID
AND x.Col1 = T1.SID )
使用LEFT JOIN尝试以下内容:
SELECT *
FROM TAB1 T1
LEFT JOIN TAB2 T2 ON T1.MemNo = T2.MemID
WHERE EXISTS ( SELECT 1
FROM TAB3 x
WHERE x.Col2 = COALESCE(T2.SVID, x.Col2)
AND x.Col1 = T1.SID )
答案 2 :(得分:0)
SELECT *
FROM TAB1 T1 INNER JOIN TAB2 T2
ON T1.MemNo = T2.MemID
AND (T1.SID in (SELECT x.Col1 FROM TAB3 x WHERE x.Col2 = T2.SVID))