查询语句一、
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<=10 order 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')
正常数据:
1 2008-10-31 7:24:57
2 2008-10-31 10:53:00
3 2008-10-31 13:41:58
4 2008-10-31 17:16:02
5 2008-11-1 8:02:09
6 2008-11-2 8:11:52
7 2008-11-1 11:44:59
8 2008-11-1 14:32:07
9 2008-11-1 17:44:29
10 2008-11-2 7:27:57
11 2008-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行数据:
1 2008-10-31 7:24:57
2 2008-10-31 10:53:00
3 2008-10-31 13:41:58
4 2008-10-31 17:16:02
5 2008-11-1 8:02:09
6 2008-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行然后排序的数据:
1 2008-11-2 8:11:52
2 2008-11-1 8:02:09
3 2008-10-31 17:16:02
4 2008-10-31 13:41:58
5 2008-10-31 10:53:00
6 2008-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 (表示取前几行后再排序)
再分享一下我老师大神的人工智能教程吧。零基础!通俗易懂!风趣幽默!还带黄段子!希望你也加入到我们人工智能的队伍中来!https://blog.csdn.net/jiangjunshow