关于Oracle中排序后取前几行的问题

查询语句一、

SELECT V.STAMP,TO_CHAR(V.GPSTIME,'YYYY-MM-DD HH24:MI:SS'),V.PHOTOURL,V.SMALLPHOTOURL,V.TOUCH_OFF,V.VIDICON_NO,V.SPEED,V.COURSE,V.STATE
FROM T_VEHICLE_PHOTO V
WHERE V.ISMANAGE = 0
AND V.TOUCH_OFF = 27
AND V.NUMBER_PLATE = '苏F14899'
AND V.STAMP < TO_DATE('2008-11-02 10:00:00', 'YYYY-MM-DD HH24:MI:SS')
AND ROWNUM <= 6
ORDER BY V.STAMP desc

查询语句二、

SELECT V.STAMP,TO_CHAR(V.GPSTIME,'YYYY-MM-DD HH24:MI:SS'),V.PHOTOURL,V.SMALLPHOTOURL,V.TOUCH_OFF,V.VIDICON_NO,V.SPEED,V.COURSE,V.STATE
FROM T_VEHICLE_PHOTO V
WHERE V.ISMANAGE = 0
AND V.TOUCH_OFF = 27
AND V.NUMBER_PLATE = '苏F14899'
AND V.STAMP < TO_DATE('2008-11-02 10:00:00', 'YYYY-MM-DD HH24:MI:SS')
AND ROWNUM <6
ORDER BY V.STAMP desc

查询语句一的结果:

STAMP

2008-11-2 8:11:52
2008-11-1 8:02:09
2008-10-31 17:16:02
2008-10-31 13:41:58
2008-10-31 10:53:00
2008-10-31 7:24:57

查询语句二的结果:

STAMP

2008-11-1 8:02:09
2008-10-31 17:16:02
2008-10-31 13:41:58
2008-10-31 10:53:00
2008-10-31 7:24:57

不加行数限制的结果:

2008-11-2 8:11:52
2008-11-2 7:27:57
2008-11-1 17:44:29
2008-11-1 14:32:07
2008-11-1 11:44:59
2008-11-1 8:02:09
2008-10-31 17:16:02
2008-10-31 13:41:58
2008-10-31 10:53:00
2008-10-31 7:24:57
2008-10-30 17:36:48

由此可以看出,如果不使用<=你拿到的数据会少一行,并且很奇怪是第一行,正常要想法应是最后一行才对,那么现在在比较以下两种写法:

select 标题,发布时间 from (select 标题,发布时间 from hhsys.tongzhigonggao order by 发布时间 desc) where rownum<11

这样是拿排序后的前10

select 标题,发布时间 from hhsys.tongzhigonggao where rownum<=10order by 发布时间 desc

这样的话应是拿到前10行再进行排序,但他实际上又是拿到排序后的前10行,真搞不明白。

噢,弄明白,了上面的语句写法是错误的。

正确的语句如下:

SELECT * FROM (
SELECT V.STAMP,TO_CHAR(V.GPSTIME,'YYYY-MM-DD HH24:MI:SS'),V.PHOTOURL,V.SMALLPHOTOURL,V.TOUCH_OFF,V.VIDICON_NO,V.SPEED,V.COURSE,V.STATE
FROM T_VEHICLE_PHOTO V
WHERE V.ISMANAGE = 0
AND V.TOUCH_OFF = 27
AND V.NUMBER_PLATE = '苏F14899'
AND V.STAMP < TO_DATE('2008-11-02 10:00:00', 'YYYY-MM-DD HH24:MI:SS')
ORDER BY V.STAMP DESC
) WHERE ROWNUM <= 6

也就是,如果你要取排序后的结果,不能将ROWNUM <= 6放在order by前面。

=======================================================================

SELECT V.STAMP,TO_CHAR(V.GPSTIME,'YYYY-MM-DD HH24:MI:SS'),V.PHOTOURL,V.SMALLPHOTOURL,V.TOUCH_OFF,V.VIDICON_NO,V.SPEED,V.COURSE,V.STATE
FROM T_VEHICLE_PHOTO V
WHERE V.ISMANAGE = 0
AND V.TOUCH_OFF = 27
AND V.NUMBER_PLATE = '苏F14899'
AND V.STAMP < TO_DATE('2008-11-02 10:00:00', 'YYYY-MM-DD HH24:MI:SS')

正常数据:

12008-10-31 7:24:57
22008-10-31 10:53:00
32008-10-31 13:41:58
42008-10-31 17:16:02
52008-11-1 8:02:09
62008-11-2 8:11:52
72008-11-1 11:44:59
82008-11-1 14:32:07
92008-11-1 17:44:29
102008-11-2 7:27:57
112008-10-30 17:36:48

===================================================

SELECT V.STAMP,TO_CHAR(V.GPSTIME,'YYYY-MM-DD HH24:MI:SS'),V.PHOTOURL,V.SMALLPHOTOURL,V.TOUCH_OFF,V.VIDICON_NO,V.SPEED,V.COURSE,V.STATE
FROM T_VEHICLE_PHOTO V
WHERE V.ISMANAGE = 0
AND V.TOUCH_OFF = 27
AND V.NUMBER_PLATE = '苏F14899'
AND V.STAMP < TO_DATE('2008-11-02 10:00:00', 'YYYY-MM-DD HH24:MI:SS')
AND ROWNUM <= 6

取前6行数据:
12008-10-31 7:24:57
22008-10-31 10:53:00
32008-10-31 13:41:58
42008-10-31 17:16:02
52008-11-1 8:02:09
62008-11-2 8:11:52

=========================================================

SELECT V.STAMP,TO_CHAR(V.GPSTIME,'YYYY-MM-DD HH24:MI:SS'),V.PHOTOURL,V.SMALLPHOTOURL,V.TOUCH_OFF,V.VIDICON_NO,V.SPEED,V.COURSE,V.STATE
FROM T_VEHICLE_PHOTO V
WHERE V.ISMANAGE = 0
AND V.TOUCH_OFF = 27
AND V.NUMBER_PLATE = '苏F14899'
AND V.STAMP < TO_DATE('2008-11-02 10:00:00', 'YYYY-MM-DD HH24:MI:SS')
AND ROWNUM <= 6
ORDER BY V.STAMP DESC
取前6行然后排序的数据:

12008-11-2 8:11:52
22008-11-1 8:02:09
32008-10-31 17:16:02
42008-10-31 13:41:58
52008-10-31 10:53:00
62008-10-31 7:24:57

总结:

取排序后的前几行,应该用:

select * from(select * from test order by stamp desc) where rownum<= 6 (表示排序后取前几行)

而不应该使用:

select * from test where rownum<= 6 order by stamp desc (表示取前几行后再排序)

你可能感兴趣的:(oracle)