首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > Mysql >

怎么快速查询在一张表但不在另外一张表中的数据

2012-02-10 
如何快速查询在一张表但不在另外一张表中的数据?表a:id{0,1,2,3,4,5,6,7,8,9}表b:id{0,1,2,3,4,5,6,7}想得

如何快速查询在一张表但不在另外一张表中的数据?
表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是最快的方法

热点排行