日期:2014-05-16  浏览次数:21022 次

如何快速查询在一张表但不在另外一张表中的数据?
表a:id{0,1,2,3,4,5,6,7,8,9}
表b:id{0,1,2,3,4,5,6,7}
想得到表a中的{8,9}两行,我用了:
1、select * from a where id not in (select id from b);
2、select * from a left join b on (b.id=a.id) where isnull(b.id);
相比之下,查询2要快一点。有没有另外什么更快的方法?

------解决方案--------------------
SQL code
select * from a where not exists (select 1 from b where b.id=a.id)

------解决方案--------------------
如两表都按ID建立索引,查询2是最快的方法