1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > mysql使用exists in distinct区别

mysql使用exists in distinct区别

时间:2019-05-07 06:11:06

相关推荐

mysql使用exists in distinct区别

使用exists

使用exists代替in

1.exists只检查行的存在性,in 检查实际的值,所以exists的性能比in好

验证

select * from emp where deptno in(select deptno from dept where loc='NEW YORK');select * from emp ewhere exists(select 1 from dept d where d.deptno=e.deptno and loc='NEW YORK');

使用exists代替distinct

1.exists只检查行的存在性,distinct用于禁止重复行的显示,而且distinct在禁止重复行的显示前需要排序检索的行,所以exists的性能比distinct好

验证

select distinct e.deptno,d.dname from emp e,dept dwhere e.deptno=d.deptno;select d.deptno,d.dname from dept dwhere exists(select 1 from emp e where e.deptno=d.deptno);

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。