ICode9

精准搜索请尝试: 精确搜索
首页 > 数据库> 文章详细

Oracle子查询与分页查询

2021-09-13 20:04:11  阅读:180  来源: 互联网

标签:salary 分页 -- 查询 Oracle rownum where select


子查询

子查询是指嵌套在查询语句中的查询语句。子查询出现的位置一般为条件语句,如WHERE条件。Oracle会首先执行子查询,然后执行父查询。

子查询整个结果集会和父结果集中每个结果进行预算,通常效率比较低,因此建议在实际应用中如果能够不使用子查询则尽量较少使用。

 

查询复制填充表

查询复制数据填充新建表

CREATE TABLE backupStu AS SELECT name,age FROM students2

 

查询复制数据填充存在表

INSERT INTO copyTab SELECT name,age FROM students2

 

分页查询

分页的目的在于将过多符合条件的结果记录按照自定义数量显示数据,从而减少内存开销并提高查询效率

Oracle中通常使用联合、子查询以及结合伪列rowid和rownum实现对结果进行分页

 

minus结合rownum查询分页

select * from students2 where rownum <=5 minus

select * from students2 where rownum <1 --第1页

 

select * from students2 where rownum <=10 minus

select * from students2 where rownum <6 --第2页

 

select * from students2 where rownum <=15 minus

select * from students2 where rownum <11 --第3页

 

select * from students2 where rownum <=20 minus

select * from students2 where rownum <16 --第4页

 

子查询结合rownum查询分页

select * from(

       (select rownum rn,s2.* from (select * from salary )s2 where rownum <=5)) s3

where s3.rn >=1;       --第1页

 

select * from(

       (select rownum rn,s2.* from (select * from salary )s2 where rownum <=10)) s3

where s3.rn >=6;       --第2页

 

select * from(

       (select rownum rn,s2.* from (select * from salary )s2 where rownum <=15)) s3

where s3.rn >=11;       --第3页

 

 

--子查询
--哪些部门没有员工
select ID 部门id,NAME 部门名称 FROM dep_table where id not in (select dep_id from emp_tab );

--哪些员工发放过工资
select ID,NAME FROM emp_tab WHERE ID IN (select emp_id from salary);
--哪些员工未发放过工资
select ID,NAME FROM emp_tab WHERE ID NOT IN (select emp_id from salary);

--查询数据填充新建表
SELECT * FROM salary;
DROP TABLE backupsalary;

CREATE TABLE backupSalary AS
SELECT emp_id ID,grantdate 发放年月,should 应发工资 FROM salary s;

select 应发工资 from backupSalary;

--查询数据填充已存在的表

CREATE TABLE back_sal
(
 ID NUMBER NOT NULL PRIMARY KEY,
 yingfa NUMBER
)

INSERT INTO back_sal select ID,salary.should from salary;
select * from back_sal;

--minus 结合rownum实现分页查询
select * from salary;
--第1页
select * from salary where rownum <=5
MINUS SELECT * FROM salary WHERE ROWNUM<1;
--第2页
select * from salary where rownum <=10
MINUS SELECT * FROM salary WHERE ROWNUM<6;
--第3页
select * from salary where rownum <=15
MINUS SELECT * FROM salary WHERE ROWNUM<11;

--子查询结合rownum实现分页查询
select * from(
       (select rownum rn,s2.* from (select * from salary )s2 where rownum <=5)) s3
where s3.rn >=1;       --第1页

select * from(
       (select rownum rn,s2.* from (select * from salary )s2 where rownum <=10)) s3
where s3.rn >=6;       --第2页

select * from(
       (select rownum rn,s2.* from (select * from salary )s2 where rownum <=15)) s3
where s3.rn >=11;       --第3页
       
--使用子查询结合rowid,rownum not in语句实现分页
select * from salary where rowid not in (select rowid from salary
where rownum<1) and rownum <=5; --第一页

select * from salary where rowid not in (select rowid from salary
where rownum<6) and rownum <=5; --第二页

select * from salary where rowid not in (select rowid from salary
where rownum<11) and rownum <=5; --第三页

 

标签:salary,分页,--,查询,Oracle,rownum,where,select
来源: https://www.cnblogs.com/zengyu1234/p/15264567.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有