取出sql表中第31到40的記錄(以自動增長id為主鍵)
sql server
方案1:
select top 10 * from t where id not in (select top 30 id from t order by id ) orde by id
sql server
方案2:
select top 10 * from t where id in (select top 40 id from t order by id) order by id desc
mysql
方案:select * from t order by id limit 30,10
oracle
方案:select * from (select rownum r,* from t where r<=40) where r>30 1.
分頁技術
1(直接利用
sql語句進行分頁,效率最高和最推薦的)
pagesize=20;
pageno = 5;
mysql:sql = "select * from articles limit " + (pageno-1)*pagesize + "," + pagesize;
oracle: sql = "select * from " +
"(select rownum r,* from " +
"(select * from articles order by postime desc)" +
"where rownum<= " + pageno*pagesize +") tmp " +
"where r>" + (pageno-1)*pagesize;
注釋:第
7行保證
rownum
的順序是確定的,因為
oracle
的索引會造成
rownum
返回不同的值
order by
時,rownum
按順序輸出,一旦有了
order by
,rownum
不按順序輸出了,這說明
rownum
是排序前的編號。如果對
order by
從句中的字段建立了索引,那麼,
rownum
也是按順序輸出的,因為這時候生成原始的查詢結果集時會參照索引表的順序來構建。
sqlserver:sql = "select top 10 * from id not id(select top " + (pageno-1)*pagesize + "id from articles)"
datasource ds = new initialcontext().lookup(jndiurl);
connection cn = ds.getconnection();
preparedstatement pstmt = cn.preparesatement(sql);
resultset rs = pstmt.executequery()
while(rs.next())
2.不可滾動的游標
pagesize=20;
pageno = 5;
cn = null
stmt = null;
rs = null;
tryint i=0;
while(rs.next() && i<10)
}cacth(){}
finnaly
catch(exception e){}
if(stm.........
if(cn............ }
3.可滾動的游標
pagesize=20;
pageno = 5;
cn = null
stmt = null;
rs = null;
try}
cacth(){}
finnaly
catch(exception e){}
if(stm.........
if(cn............ }
常用資料庫分頁語句
select from cat limit select from cat limit 20000,100 從cat表中第20000條開始取出100條記錄,即記錄號為20000到20099的記錄 select from cat limit select from cat limit 100 從cat...
資料庫分頁語句
1 mysql select from tb user order by id asc limit 10 offset 10 size small color blue limit指定每頁大小,offset指定偏移量,在程式 中可以寫成這樣 select from tb user order by ...
資料庫分頁語句
取出sql表中第31到40的記錄 以自動增長id為主鍵 sql server方案1 select top 10 from t where id not in select top 30 id from t order by id orde by id sql server方案2 select top...