oracle rownum语法问题

select * from (select p.*,rownum from prod.product_offer p)
where rownum = (select count(*)/2 from prod.product_offer);
这样为什么什么都查不出来
select * from (select p.*,rownum id from prod.product_offer p)
where id = (select count(*)/2 from prod.product_offer);
而在rownum加了一个别名后就可以查询出来,这是怎么回事,望高手解答

第一句中两个rownum其实不是一回事了. where条件里面的rownum是为 (select p.*,rownum from prod.product_offer p) 这个语句产生的结果集产生的伪列. 而rownum=后面除非是1 ,是大于1的数都不会产生任何结果集. (举个例子,假设判断rownum=2,先判断第一列rownum分配1,结果不符合条件,接着第二条,还是rownum给分配1,还是不符合条件,所以这样下去所有的记录都是不符合条件的). 一般的用法是rownum< 一个数值

第二句中两个id指的是同一回事,所以能查出来.
记住rownum是伪列,不是实际存在的列就可以了.
温馨提示:答案为网友推荐,仅供参考
第1个回答  2010-12-15
rownum只能小于某个数值或者小于等于某个数值
等于、大于、大于等于都不行
select t.id from student t where rownum<2;
select t.id from student t where rownum<=2;
这样才会有结果
select t.id from student t where rownum=2;
select t.id from student t where rownum>2;
select t.id from student t where rownum>=2;
是不会有结果的
加上别名后在外层查询中就成了一个普通列了,就可以等于、大于、大于等于了
相似回答