`
peijunlin2008
  • 浏览: 166298 次
  • 性别: Icon_minigender_1
  • 来自: 河北省
社区版块
存档分类
最新评论

not in 与 not exists ,in 与 exists 的区别

阅读更多
in 与 exists  的区别:

语句1:
select ename from emp e where empno in (select mgr from emp);


语句2:
select ename from emp e where  exists (select 1 from emp where mgr=e.empno);


区别:in 的 语句查询会循环查询,exists 只查询一次即可,exists 查询效率较高

not in 与 not  exists的区别

语句1:
select ename from emp e where empno  not in (select mgr from emp);

由于当not in 中存在null的话讲查询出所有值

语句1修改后:
select ename from emp e where empno  not in (select mgr from emp where mgr is not null );


语句2:
select ename from emp e where not  exists (select 1 from emp where mgr=e.empno);


not  exists可以避免null值的调用,not in时刻需要考虑空值的情况,同时not  exists的效率要远远高于not in;

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics