sql分页查询

Posted on 2015-03-24 17:23  云起  阅读(9)  评论(0)    收藏  举报  来源

想用sql做简单的分页查询,但是用rownum的时候,会出现问题。

select * from (
    select t.*,rownum from (
        select * from tablename where condition order by columnname) t )
    where rownum>(pagecount-1)*5 and rownum<=(pagecount)*5
(表名,condition筛选条件,columnname排序的列名,pagecount页数)

然后看了下帖的解释才明了。

http://blog.csdn.net/lg312200538/article/details/4587455

主要是因为rownum是伪列,需要从1开始,所以做了修改

select * from (
    select t.* ,rownum as rn from (
        select * from tablename where condition order by columnname) t )
        where rn between (pagecount-1)*5 and (pagecount*5)





博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3