设为首页 加入收藏

TOP

Oracle子查询相关内容(包含TOP-N查询和分页查询)
2018-08-31 18:27:12 】 浏览:83
Tags:Oracle 查询 相关 内容 包含 TOP-N


select deptno,dname,(select count(*) from emp e where e.deptno=d.deptno) amount from dept d;



select ename,sal,e.deptno from emp e,(select deptno,avg(sal) avgsal from emp group by deptno) m
where e.deptno=m.deptno and e.sal>m.avgsal;



select * from emp where job=(select job from emp where ename='SCOTT');



select deptno,avg(sal) from emp group by deptno having avg(sal)>(select max(sal) from emp where deptno=30);


select * from emp where deptno in(select deptno from dept where dname='RESEARCH' or dname='SALES');


select ename,sal,deptno from emp where sal>all(select sal from emp where deptno=30);



select ename,sal,deptno from emp where sal>any(select sal from emp where deptno=30);



select * from (select * from emp where deptno in(20,30) order by sal desc) where rownum<=5;



select * from emp e where e.deptno in(20,30) and rownum<=5 order by e.sal desc;



select * from (select rownum rowline,emp1.* from (select * from emp where deptno in (20,30)
order by sal) emp1 where rownum<=10) emp2 where emp2.rowline>=7 and emp2.rowline<=8;



select * from (select * from emp where deptno in(20,30) order by sal desc) where rownum>=7 and rownum<=8;


外层查询判rownum>=7 and rownum<=8不符合条件去除记录,当第二条记录产生时,oracle仍然会为其伪列赋值rownum=1,


外层判断仍然不会通过,这样无论内层查询产生多少数据都会因为外层查询的条件不符合记录而流失数据。


而想要避免这样的情况发生,就需要将伪列当成一个查询中的字段,将它不在看做“伪列”,而是真正的一个字段,


这样就需要在外面在嵌套一层查询将伪列做成一个物理上存在的字段,而最后我们只需要将外层查询的条件改为内层查询中“真实”存在的伪列即可。


子查询的相关内容总结完毕,有不明处请多多指教。


】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇Oracle触发器详细讲解 下一篇Linux下Oracle 12C R2图形化安装..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目