Oracle与DB2、MySQL取前10条记录的对比

Oralce的示例:
1. 最佳选择:利用分析函数
row_number() over ( partition by col1 order by col2 )
比如想取出100-150条记录,按照tname排序

select tname,tabtype from (
select tname,tabtype,row_number() over ( order by tname ) rn from tab
)
where rn between 100 and 150;
2. 使用rownum 虚列
select tname,tabtype from (
select tname,tabtype,rownum rn from tab where rownum <= 150
)
where rn >= 100;
注释:使用序列时不能基于整个记录集合来进行排序,假如指定了order by子句,排序的的是选出来的记录集的排序。
create table mynumber(id int,name varchar(10));
insert into mynumber values(1,'no1');
insert into mynumber values(2,'no2');
insert into mynumber values(3,'no3');
insert into mynumber values(4,'no4');
insert into mynumber values(5,'no5');
insert into mynumber values(5,'no6');
insert into mynumber values(6,'no7');
insert into mynumber values(7,'no8');
insert into mynumber values(8,'no9');
insert into mynumber values(9,'no10');
insert into mynumber values(9,'no11');
insert into mynumber values(9,'no12');
insert into mynumber values(10,'no13');
insert into mynumber values(10,'no14');
insert into mynumber values(10,'no15');
insert into mynumber values(11,'no16');
insert into mynumber values(12,'no17');
insert into mynumber values(13,'no18');
select id,name,RANK() over ( order by id ) case1,
DENSE_RANK() over ( order by id ) case2,
row_number() over ( order by id ) case3 from mynumber;
(1) 取前10条不同id记录,假如最后1条记录的ID依然有相同的,那么取出来。
select id,name from mynumber where id in
(select id from (select distinct id
from mynumber) tt where rownum<=10);

select * from (select id,name,RANK()
over ( order by id ) case1,DENSE_RANK()
over ( order by id ) case2,row_number()
over ( order by id ) case3 from mynumber)
tt where case1<=10;
select * from (select id,name,RANK() over
( order by id ) case1,DENSE_RANK() over
( order by id ) case2,row_number() over
( order by id ) case3 from mynumber)
tt where case1 between 5 and 10;
(2)取前10条记录,假如第10条记录的ID 还有相同的,那么取出来。
select * from mynumber where id in
(select id from mynumber where rownum <=10);

select * from (select id,name,RANK()
over ( order by id ) case1,DENSE_RANK()
over ( order by id ) case2,row_number()
over ( order by id ) case3 from mynumber)
tt where case2<=10;
select * from (select id,name,RANK()
over ( order by id ) case1,DENSE_RANK()
over ( order by id ) case2,row_number()
over ( order by id ) case3 from mynumber)
tt where case2 between 5 and 10;

(3)取前10条记录
select id,name from mynumber
where rownum <=10;
select id,name from
(select id,name,rownum rn from
mynumber where rownum <= 10 ) where rn >= 5;

select * from (select id,name,RANK()
over ( order by id ) case1,DENSE_RANK()
over ( order by id ) case2,row_number()
over ( order by id ) case3 from mynumber)
tt where case3<=10;
select * from (select id,name,RANK()
over ( order by id ) case1,DENSE_RANK()
over ( order by id ) case2,row_number()
over ( order by id ) case3 from mynumber)
tt where case3 between 5 and 10;
Db2示例
create table mynumber(id int,name varchar(10))
insert into mynumber values(1,'no1')
insert into mynumber values(2,'no2')
insert into mynumber values(3,'no3')
insert into mynumber values(4,'no4')
insert into mynumber values(5,'no5')
insert into mynumber values(5,'no6')
insert into mynumber values(6,'no7')
insert into mynumber values(7,'no8')
insert into mynumber values(8,'no9')
insert into mynumber values(9,'no10')
insert into mynumber values(9,'no11')
insert into mynumber values(9,'no12')
insert into mynumber values(10,'no13')
insert into mynumber values(10,'no14')
insert into mynumber values(10,'no15')
insert into mynumber values(11,'no16')
insert into mynumber values(12,'no17')
insert into mynumber values(13,'no18')
select * from (select id,name,RANK() over
( order by id ) case1,DENSE_RANK() over
( order by id ) case2,row_number() over
( order by id ) case3 from mynumber) as tt where case1<=10
(1) 取前10条不同id记录,假如最后1条记录的ID依然有相同的,那么全部取出来。
select * from mynumber where id in
(select distinct id from mynumber fetch first 10 rows only)

select * from (select id,name,RANK()
over ( order by id ) case1,DENSE_RANK()
over ( order by id ) case2,row_number()
over ( order by id ) case3 from mynumber)
as tt where case1<=10
select * from (select id,name,RANK()
over ( order by id ) case1,DENSE_RANK()
over ( order by id ) case2,row_number()
over ( order by id ) case3 from mynumber)
as tt where case1 between 5 and 10
(2)取前10条记录,假如第10条记录的ID 还有相同的,那么全部取出来。
select * from mynumber where id in
(select id from mynumber fetch first 10 rows only)

select * from (select id,name,RANK()
over ( order by id ) case1,DENSE_RANK()
over ( order by id ) case2,row_number()
over ( order by id ) case3 from mynumber)
as tt where case2<=10
select * from (select id,name,RANK()
over ( order by id ) case1,DENSE_RANK()
over ( order by id ) case2,row_number()
over ( order by id ) case3 from mynumber)
as tt where case2 between 5 and 10
(3)取前10条记录
select id from mynumber fetch first 10 rows only

select * from (select id,name,RANK() over
( order by id ) case1,DENSE_RANK() over
( order by id ) case2,row_number() over
( order by id ) case3 from mynumber) as tt where case3<=10
select * from (select id,name,RANK()
over ( order by id ) case1,DENSE_RANK()
over ( order by id ) case2,row_number()
over ( order by id ) case3 from mynumber)
as tt where case3 between 5 and 10
Mysql示例:
select id from mytable order by update_date desc limit 0,10


ORACLE中取前N条记录
(2009-11-11 14:30:48)
转载
标签:
from
name
order
by
oracle
it
分类: 技术浅谈

1.在ORACLE中实现SELECT TOP N
由于ORACLE不支持SELECT TOP语句,所以在ORACLE中经常是用ORDER BY跟ROWNUM的组合来实现SELECT TOP N的查询
简单地说,实现方法如下所示:
SELECT 列名1...列名n FROM
(SELECT 列名1...列名n FROM 表名 ORDER BY 列名1...列名n)
WHERE ROWNUM <= N(抽出记录数)
ORDER BY ROWNUM ASC

下面举个例子简单说明一下

顾客表customer(id,name)有如下数据:
ID NAME
01 first
02 Second
03 third
04 forth
05 fifth
06 sixth
07 seventh
08 eighth
09 ninth
10 tenth
11 last

则按NAME的字母顺抽出前三个顾客的SQL语句如下所示:
SELECT * FROM
(SELECT * FROM CUSTOMER ORDER BY NAME)
WHERE ROWNUM <= 3
ORDER BY ROWNUM ASC

输出结果为:
ID NAME
08 eighth
05 fifth
01 first

2.在TOP N纪录中抽出第M(M <= N)条记录
在得到了TOP N的数据之后,为了抽出这N条记录中的第M条记录,我们可以考虑从ROWNUM着手。我们知道,ROWNUM是记录表中数据编号的一个隐藏字段,所以可以在得到TOP N条记录的时候同时抽出记录的ROWNUM,然后再从这N条记录中抽取记录编号为M的记录,即使我们希望得到的结果

从上面的分析可以很容易得到下面的SQL语句
SELECT 列名n...列名n FROM
(SELECT ROWNUM RECNO, 列名n...列名n FROM
(SELECT 列名1...列名n FROM 表名 ORDER BY 列名1...列名n)
WHERE ROWNUM <= N(抽出记录数)
ORDER BY ROWNUM ASC )
WHERE RECNO = M(M <= N)

同样以上表的数据为基础,那么得到以NAME的字母顺排序的第二个顾客的信息的SQL语句应该这样写:
SELECT ID, NAME FROM
(SELECT ROWNUM RECNO, ID, NAME FROM
(SELECT * FROM CUSTOMER ORDER BY NAME)
WHERE ROWNUM <= 3
ORDER BY ROWNUM ASC)
WHERE RECNO = 2

结果则为:
ID NAME
05 fifth
3.抽出按某种方式排序的记录集中的第N条记录
在2的说明中,当M = N的时候,即为我们的标题讲的结果
实际上,2的做法在里面N>M的部分的数据是基本上不会用到的,我们仅仅是为了说明方便而采用
如上所述,则SQL语句应为:

SELECT 列名1...列名n FROM
(SELECT ROWNUM RECNO, 列名1...列名n FROM
(SELECT 列名1...列名n FROM 表名 ORDER BY 列名1...列名n)
WHERE ROWNUM <= N(抽出记录数)
ORDER BY ROWNUM ASC )
WHERE RECNO = N

那么,2中的例子的SQL语句则为:
SELECT ID, NAME FROM
(SELECT ROWNUM RECNO, ID, NAME FROM
(SELECT * FROM CUSTOMER ORDER BY NAME)
WHERE ROWNUM <= 2
ORDER BY ROWNUM ASC )
WHERE RECNO = 2
结果为:
ID NAME
05 fifth

4.抽出按某种方式排序的记录集中的第M条记录开始的X条记录
3里所讲得仅仅是抽取一条记录的情况,当我们需要抽取多条记录的时候,此时在2中的N的取值应该是在
N >= (M + X - 1)这个范围内,当让最经济的取值就是取等好的时候了的时候了。当然最后的抽取条件也不是
RECNO = N了,应该是RECNO BETWEEN M AND (M + X - 1)了,所以随之而来的SQL语句则为:

SELECT 列名1...列名n FROM
(SELECT ROWNUM RECNO, 列名1...列名n FROM
(SELECT 列名1...列名n FROM 表名 ORDER BY 列名1...列名n)
WHERE ROWNUM <= N (N >= (M + X - 1))
ORDER BY ROWNUM ASC)
WHERE RECNO BETWEEN M AND (M + X - 1)

同样以上面的数据为例,则抽取NAME的字母顺的第2条记录开始的3条记录的SQL语句为:

SELECT ID, NAME FROM
(SELECT ROWNUM RECNO, ID, NAME FROM
(SELECT * FROM CUSTOMER ORDER BY NAME)
WHERE ROWNUM <= (2 + 3 - 1)
ORDER BY ROWNUM ASC )
WHERE RECNO BETWEEN 2 AND (2 + 3 - 1)

结果如下:
ID NAME
05 fifth
01 first
04 forth

以此为基础,再扩展的话,做成存储过程,将开始记录数以及抽取记录数为参数,就可以轻松实现分页抽取数据。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值