1、创建测试表,
create table test_row(id number , value varchar2(20));
2、插入测试数据
insert into test_row select 20+level as id, 'value_'||level from dual connect by level<=30;
commit;
3、查询表中全量数据,select t.* from test_row t;
4、编写sql,根据ID进行排序,查询第5条数据到8条数据;
select * from (select t.*, row_number() over(order by id) rn from test_row t )
where rn >= 5 and rn <= 8;
oracle数据库中用sql查询某表(ur_table_name )的第5到第8条数据的语句为:
SELECT *
FROM (SELECT a.*, ROWNUM rn
FROM ur_table_name a)
WHERE rn >= 5 AND rn <= 8 ;
之所以用rownum rn,是把rownum转成实例,因为rownum本身只能用 <=的比较方式,只有转成实列,才可做 >=的比较。
select * from (select row_number() over(order by 你要进行排序的列) as RowID from 表) as T where RowID between 5 and 8
select * from (select t.*,row_number() over(order by t.id) as rn from table1 t)
where rn between 5 and 8;
--table1: 表名
--order by t.id 按什么字段排序
select * from (select * ,rownum row from table where rownum<=8) a
where a.row<=5
都这么简洁,你还嫌麻烦,我无语了……