日期:2014-05-16  浏览次数:20471 次

返回奇数行或者偶数行数据库表记录

--方法1用Decode函数

for even number of records

select*from emp where rowid in(select decode(mod(rownum,2),0,rowid) from emp);

for odd number of records

select*from emp where rowid in(select decode(mod(rownum,2),1,rowid) from emp);



或者:

FOR ODD NUMBER OF ROWS

SELECT*FROM emp WHERE rowid IN(SELECT DECODE(MOD(rowid,2),1,rowid,NULL)FROM emp);



FOR EVEN NUMBER OF ROWS
SELECT*FROM emp WHERE rowid IN(SELECT DECODE(MOD(rowid,2),0,rowid,NULL) FROM emp);



--方法2用 in 子查询

Odd number of records:
select * from emp where (rowid,1) in (select rowid, mod(rownum,2) from emp);
Output:-
1
3
5
Even number of records:
select * from emp where (rowid,0) in (select rowid, mod(rownum,2) from emp)
Output:-
2
4
6


?