ORACLE查询某张表奇数行或者偶数行数据

时间:2024-04-07 17:31:39

说明:

floor函数:向下取整,比如floor(2.5) = 2;floor(3.0) = 3;

查询:

select floor(e2.r/ 2),floor((e2.r-1)/2),e2.* from
(select rownum r, e1.* from
  (
  select * from nhlh_sto_yggys_mid where f_pkey in (select f_pkey from nhlh_sto_yggys_mid group by f_pkey having count(1) >1) order by f_pkey
  ) e1
) e2 where floor(e2.r/ 2) >floor((e2.r-1)/2) ;

结果:

ORACLE查询某张表奇数行或者偶数行数据

解释:因为表中没有行数列,所以首先我们查询加入行数

select rownum r, e1.* from
  (
  select * from nhlh_sto_yggys_mid where f_pkey in (select f_pkey from nhlh_sto_yggys_mid group by f_pkey having count(1) >1) order by f_pkey
  ) e1

然后我们通过取整函数判断行数除2与行数减1除2的结果对比,从而查询出所需结果。查询奇数行也是一样的道理。