在網(wǎng)頁上顯示大量數(shù)據(jù)的時候,很多時候是要求按一定的順序分頁來顯示.大家可能都知道用oracle中的rownum可以實現(xiàn)分頁查詢,比如:
select * from tablename where rownum<=10.
?但是如果要排序的話,如下的語句:
select * from tablename where rownum<=10 order by name.
卻不能達(dá)到我們想要的效果,這個語句執(zhí)行結(jié)果是取了10條數(shù)據(jù),然后排序.而不是先排序再取前10條.這樣就需要我們加入子查詢:
select * from (select * from tablename order by name) where rownum<10
另外rownum的使用是不允許我們用">"的,如果我要顯示第二頁,即10-20條數(shù)據(jù),不能用
select * from (select * from tablename order by name) where? rownum>10 and rownum<20
或
select * from (select * from tablename order by name) where? rownum between 10 and 20
這樣的話我們只能另想辦法解決,解決方法如下:
select * from(
select rownum rowno,t.* from
?(select *
?from?tablename
?order by name) t
where rownum<6)where rowno>2;
但是這樣的話也會影響到速度,各位如果有好的方法可以一起探討下.
select * from tablename where rownum<=10.
?但是如果要排序的話,如下的語句:
select * from tablename where rownum<=10 order by name.
卻不能達(dá)到我們想要的效果,這個語句執(zhí)行結(jié)果是取了10條數(shù)據(jù),然后排序.而不是先排序再取前10條.這樣就需要我們加入子查詢:
select * from (select * from tablename order by name) where rownum<10
另外rownum的使用是不允許我們用">"的,如果我要顯示第二頁,即10-20條數(shù)據(jù),不能用
select * from (select * from tablename order by name) where? rownum>10 and rownum<20
或
select * from (select * from tablename order by name) where? rownum between 10 and 20
這樣的話我們只能另想辦法解決,解決方法如下:
select * from(
select rownum rowno,t.* from
?(select *
?from?tablename
?order by name) t
where rownum<6)where rowno>2;
但是這樣的話也會影響到速度,各位如果有好的方法可以一起探討下.
select top (pageNum-1)*pageSize id from table
where xxx=xxx
order by xxx,yyy
) order by xxx,yyy