0

我试图在 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

谢谢

4

3 回答 3

2

尝试存在:

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)
于 2013-07-26T07:32:14.410 回答
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 )
于 2013-07-26T07:35:16.837 回答
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))
于 2013-07-26T07:34:25.923 回答