您当前的位置: 首页 >  oracle

qq_34412985

暂无认证

  • 0浏览

    0关注

    1061博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

Oracle中的rownum 和rowid的用法和区别

qq_34412985 发布时间:2019-12-03 16:19:23 ,浏览量:0

1.rownum是伪列,是在获取查询结果集后再加上去的 (获取一条记录加一个rownum)。对符合条件的结果添加一个从1开始的序列号。

   eg:  

select rownum,phone_no from ur_user_info where rownum < 6;

attention:

  rownum是动态的,必有查询结果,然后再给查询的结果集添加上这个列。  例如:第一条记录的rownum是1 ,第二条是2,以此类推。 

select rownum, phone_no from ur_user_info where rownum > 5 and rownum < 10;  ---查询结果为空集

当产生结果集时,oracle会产生一条rownum为1的记录,显然不符合条件;那么就会产生第二条记录,同样rownum=1,也不符合记录;  一直下去,导致最后上述sql产生的结果集时空集。 

如果需要查询到结果,需要使用子查询:

-----错误写法:
select rownum, phone_no
  from (select rownum rn , phone_no from ur_user_info ) a
 where a.rownum > 5  and a.rownum < 10;

-----正确写法:
select rownum,*
  from (select rownum rn, a.* from ur_user_info a where rownum < 10) a
 where a.rn > 5;

--注意: 1.rownum只能用< 或者            
关注
打赏
1653291990
查看更多评论
0.1686s