我有3个故事,分别是表A,表B和表C,它们的表结构相同。 除了在表A或表B中找不到的记录之外,在表C中显示所有记录的最快方法是什么
表A
IDNO
------
A100
A200
A300
表B
IDNO
------
T555
表C
IDNO
-------
A100
A200
A300
T555
X999
预期产量
RECNO
------
A100
A200
A300
T555
这是我的脚本,但不确定是否是最快的脚本。
SELECT a.*
FROM TABLEC as a LEFT OUTER JOIN
TABLEA as a on a.[RECNO] = b.[RECNO] LEFT OUTER JOIN
TABLEB as b on a.[RECNO] = c.[RECNO]
WHERE b.[RECNO] IS NOT NULL Or
c.[RECNO] IS NOT NULL
答案 0 :(得分:1)
这很容易,我会这样做:
select * from Tablec
where not exists(select * from tablea where Tablea.idno=TableC.IDNO)
or not exists (select * from tableb where Tableb.idno=TableC.IDNO)
只需确保您具有适当的索引即可。
答案 1 :(得分:1)
确保您在RECNO上具有正确的索引
SELECT c.*
FROM TABLEC as c
WHERE NOT
(
NOT EXISTS (SELECT * FROM TABLEA a WHERE a.RECNO = c.RECNO)
AND NOT EXISTS (SELECT * FROM TABLEB b WHERE b.RECNO = c.RECNO)
)