select distinct(列A) from table where rownum<=2 这样子不行 它优先执行ROWNUM 达不到我要的效果 列A 1 1 2 2 3 3 上面的sql执行结果是1 我想要的执行结果是 1 2
嵌套 select * from( select distinct 列A from table) where rownum<=2
子查询: select * from (select distinct(列a) from table) where rownum<=2
突然就发现ORACLE不能理解啊 Select a From (Select Distinct a From table1) Where Rownum<=2; 结果 1 2 Select a From (Select Distinct a From table1) Where Rownum=0; 结果 空 Select a From (Select Distinct a From table1) Where Rownum=1; 结果1Select a From (Select Distinct a From table1) Where Rownum=2; 结果 空 有没有人能给我解释一下啊、?
rownum是赋给结果集的伪列 如果rownum=0则当然没有记录了 如果条件为rownum=2,没有rownum=1的记录,就不会有rownum=2,所以结果为空 可以用 select * from( select t.*,rownum as rn from t where rownum<=2) where rn=2
select * from( select distinct 列 from 表) where rownum=指定行数
这样子不行 它优先执行ROWNUM 达不到我要的效果
列A
1
1
2
2
3
3
上面的sql执行结果是1
我想要的执行结果是
1
2
select * from(
select distinct 列A from table)
where rownum<=2
select * from (select distinct(列a) from table) where rownum<=2
Select a From (Select Distinct a From table1) Where Rownum<=2;
结果
1
2
Select a From (Select Distinct a From table1) Where Rownum=0;
结果
空
Select a From (Select Distinct a From table1) Where Rownum=1;
结果1Select a From (Select Distinct a From table1) Where Rownum=2;
结果
空
有没有人能给我解释一下啊、?
如果rownum=0则当然没有记录了
如果条件为rownum=2,没有rownum=1的记录,就不会有rownum=2,所以结果为空
可以用
select *
from(
select t.*,rownum as rn
from t
where rownum<=2)
where rn=2
select distinct 列 from 表)
where rownum=指定行数