加入收藏 | 设为首页 | 会员中心 | 我要投稿 湖南网 (https://www.hunanwang.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长百科 > 正文

Oracle-子查询,分页

发布时间:2021-01-11 16:15:57 所属栏目:站长百科 来源:网络整理
导读:1? 子查询 1.1? 子查询 sql 中 查询是 可以嵌套的。 一个 查询可以作为其它一个查询的前提、表 。 SELECT select_list FROM table WHERE expr operator( SELECT select_list FROM table ); 领略子查询的要害在于把子查询看成一张表来对待。外层的语句可以把

Oracle-子查询,分页

?

总结

[1]?order by 必然在整个功效集呈现后才执行。

[2]?rownum 在功效集呈现后才有编号。

。。。-> select -> rownum -> order by

-- 查询凭证薪资降序,前10名雇员

select vt0.*,rownum

from (select e.*

     from emp e

     order by e.sal desc) VT0

where rownum <= 10

?

?

Oracle-子查询,分页

?

1.3?分页(A)

-- 求查询6-10号的雇员

select vt0.*

from (select e.*,rownum "RN"

        from emp e

        where rownum <= 10) VT0

where vt0.rn >= 6

求page=n,pagesize=size的数据

=>[(n-1)*size+1,n*size]

select vt0.*

from (select t.*,rownum “RN”

from table t 

where rownum <= n*size) VT0

where vt0.rn >= (n-1)*size+1

1.4?行转列?(B)

?

Oracle-子查询,分页

4.1获得相同下面的功效

姓名 ??语文 ?数学 ?英语

张三 ???78 ???88 ???98

王五 ???89 ???56 ???89

select ts.name,sum(decode(ts.subject,‘语文‘,ts.score)) "语文",‘数学‘,ts.score)) "数学",‘英语‘,ts.score)) "英语"

from test_score ts

group by ts.name

(编辑:湖南网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

1?子查询

1.1?子查询

sql中查询是可以嵌套的。一个查询可以作为其它一个查询的前提、表。

SELECT select_list

FROM table

WHERE expr operator

(SELECT select_list

 FROM table);

领略子查询的要害在于把子查询看成一张表来对待。外层的语句可以把内嵌的子查询返回的功效当成一张表行使。子查询可以作为一个虚表被行使。

子查询要用括号括起来

将子查询放在较量运算符的右边(加强可读性)

子查询按照其返回功效可以分为单行子查询和多行子查询。

1.1.1?单行子查询

当子查询有单行时,可以取单行中的一个字段形成单个值用于前提较量。

?

-- 查询雇员其薪资在雇员均匀薪资以上

-- [1] 查询员工的均匀薪资

select avg(e.sal) "AVGSAL"

from emp e

 

--[2] 查询满意前提的雇员

select *

from emp e

where e.sal > (select avg(e.sal) "AVGSAL" from emp e)

1.1.2?多行子查询

?

-- 查在雇员中有哪些人是打点者

--【1】查询打点者

select distinct e.mgr

from emp e

where e.mgr is not null

 

--【2】查询指定列表的信息 in

select e.*

from emp e

where e.empno in (select distinct e.mgr

                         from emp e

                         where e.mgr is not null)

多行子查询返回的功效可以作为 表?行使,凡是团结in、some/any、all、exists。

1.1.3?From后的子查询

子查询可以作为一张续表用于from后。

?

-- 每个部分均匀薪水的品级

--【1】部分的均匀薪资

select e.deptno,avg(e.sal) "AVGSAL"

from emp e

group by e.deptno

 

--【2】求品级

select vt0.deptno,vt0.avgsal,sg.grade

from (select e.deptno,avg(e.sal) "AVGSAL"

        from emp e

        group by e.deptno) VT0,salgrade sg

where vt0.avgsal between sg.losal and sg.hisal

?

例:

-- 99 join on

select vt0.deptno,avg(e.sal) "AVGSAL"

        from emp e

        group by e.deptno) VT0 join salgrade sg on vt0.avgsal between sg.losal and sg.hisal

1.2?TOP-N(A)

把select获得的数据集提取前n条数。

rownum:暗示对查询的数据集记录的编号,从1开始。

?

-- 查询前10名雇员

select e.*,rownum

from emp e

where rownum <= 10

rownum和order-by

-- 查询凭证薪资降序,前10名雇员

select e.*,rownum

from emp e

where rownum <= 10

order by e.sal desc

?

    热点阅读