查詢分頁常用的語句

查詢分頁常用的語句
db2查詢分頁:
========
 
表中前20條記錄:select * from news where classid=4 order by id desc fetch first 20 rows only
 
表中20條記錄:
select * from
(
    select id,title,content,rownumber() over (order by id desc) as row_next
    from news
    where classid=4
) as temp where row_next between 21 and 40
 

如果去掉 row_next 子句( ROW_NEXT BETWEEN ? and ? ),那麼將返回所有匹配選擇標準的行。

上面使用的 SELECT * FROM 子句可以看作一個臨時表,裏面存有匹配選擇標準的整個結果集,然後從這個臨時表中返回落在給定行範圍內的結果集。

使用 rownumber() 功能時對系統會有額外的性能影響,因爲數據庫首先要獲取所有匹配選擇標準的行,然後再返回落在給定範圍內的那些行。

 oracle查詢分頁
==========

select * from
(
    select rownum num,title,conten
  from new
    where rownum <= 20
    order by id desc
)
where num > 10

 hibernate分頁
==========

Query q = session.createQuery("from Cat as c");
q.setFirstResult(20000);
q.setMaxResults(100);
List l = q.list();

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章