sql语句优化:用join取代not in

不要太多使用not in查询,最好用表连接来取代它。如:

select ID,name from Table_A where ID not in (select ID from Table_B)

这句是最经典的not in查询了。改为表连接代码如下:

select Table_A.ID,Table_A.name from Table_A left join Table_B on Table_A.ID=Table_B.ID and Table_B.ID is null
或者:
select Table_A.ID,Table_A.name from Table_A left join Table_B on Table_A.ID=Table_B.ID where Table_B.ID is null

文章出自:http://www.cnblogs.com/rising-fay/archive/2012/11/13/2767538.html

posted @ 2017-08-23 21:34  chenxizhaolu  阅读(1031)  评论(0编辑  收藏  举报