为什么这两个sqls给出不同的结果?

时间:2017-07-04 02:00:04

标签: sql oracle11g

我在PL / SQL中执行这些sqls。

原始的sql是:

select  * from (
  select RecID,TaskNum from umstat.tostatinfo
  where RecID in (select recid from umstat.tostatinfo where StreetID = 101 and checkNum > 0)
  order by recID desc  
) where rownum >= 1 and rownum <= 50

RecID是umstat.tostatinfo的主键。这个sql将清空列TaskNum并给出以下结果:

enter image description here

奇怪的是,如果我删除第四行(order by recID desc)或删除where(where rownum >= 1 and rownum <= 50)强加的最外层限制,那么列TaskNum将会活跃起来:

enter image description here

为了使用order byrownum提供的功能,我将原始sql简化为更简洁的一个:

select  * from (
  select RecID,TaskNum from umstat.tostatinfo
  where StreetID = 101 and checkNum > 0
  order by recID desc 
) where rownum >= 1 and rownum <= 50

这个sql的结果看起来很正常:

enter image description here

更多测试:

将子查询替换为普通ID (447914,447912,447905)

select * from (
  select RecID,TaskNum from umstat.tostatinfo
  where RecID in (447914,447912,447905)
  order by recID desc  
) where rownum >= 1 and rownum <= 50

结果是:

enter image description here

在子查询中将recid更改为TaskNum

select  * from (
  select RecID,TaskNum from umstat.tostatinfo
  where TaskNum in (select TaskNum from umstat.tostatinfo where StreetID = 101 and checkNum > 0)
  order by recID desc  
) where rownum >= 1 and rownum <= 50

结果是:

enter image description here

我想知道是什么让所有这些都发生在地下,感谢你的时间。

0 个答案:

没有答案