復(fù)制代碼 代碼如下:
create table T_NEWS
(
ID NUMBER,
N_TYPE VARCHAR2(20),
N_TITLE VARCHAR2(30),
N_COUNT NUMBER
)
prompt Disabling triggers for T_NEWS...
alter table T_NEWS disable all triggers;
prompt Loading T_NEWS...
insert into T_NEWS (ID, N_TYPE, N_TITLE, N_COUNT)
values (1, 'IT', '愛(ài)it1', 100);
insert into T_NEWS (ID, N_TYPE, N_TITLE, N_COUNT)
values (2, '體育', '愛(ài)體育1', 10);
insert into T_NEWS (ID, N_TYPE, N_TITLE, N_COUNT)
values (3, '體育', '愛(ài)體育2', 30);
insert into T_NEWS (ID, N_TYPE, N_TITLE, N_COUNT)
values (4, 'IT', '愛(ài)it2', 300);
insert into T_NEWS (ID, N_TYPE, N_TITLE, N_COUNT)
values (5, 'IT', '愛(ài)it3', 200);
insert into T_NEWS (ID, N_TYPE, N_TITLE, N_COUNT)
values (6, '體育', '愛(ài)體育3', 20);
insert into T_NEWS (ID, N_TYPE, N_TITLE, N_COUNT)
values (7, '體育', '愛(ài)體育4', 60);
commit;
第一步:我先用rownum
--分頁(yè) row_number,不是rownum
--根據(jù)n_count從大到小排列,每頁(yè)3條
SELECT ROWNUM r,t.* FROM t_news t
WHERE ROWNUM=3
ORDER BY t.n_count DESC
--問(wèn)題:為什么order by以后,行號(hào)是亂的?
SELECT ROWNUM r,t.* FROM t_news t
--原因:先分配了行號(hào),再根據(jù)n_count排序
--所以必須排序,再生成行號(hào)
SELECT ROWNUM r,t.* FROM (
SELECT t.* FROM t_news t ORDER BY t.n_count DESC ) t
--分頁(yè)
--err
SELECT ROWNUM r,t.* FROM (
SELECT t.* FROM t_news t ORDER BY t.n_count DESC ) t
WHERE r between 1 AND 3
--第1頁(yè)
SELECT ROWNUM r,t.* FROM (
SELECT t.* FROM t_news t ORDER BY t.n_count DESC ) t
WHERE ROWNUM between 1 AND 3
--第2頁(yè)
SELECT ROWNUM r,t.* FROM (
SELECT t.* FROM t_news t ORDER BY t.n_count DESC ) t
WHERE ROWNUM between 4 AND 6
--error: ROWNUM必須從1開(kāi)始!
SELECT k.* FROM (
SELECT ROWNUM r,t.* FROM (
SELECT t.* FROM t_news t ORDER BY t.n_count DESC ) t
) k
WHERE r BETWEEN 4 AND 6
--麻煩,效率低!
*****第二步:我用row_number() over()函數(shù)
select t2.* from
(select t.*,row_number()over(order by t.n_count desc) orderNumber from t_news t order by t.n_count desc)t2 where orderNumber between 1and 3;
*****************************************************************************************************************************************88
SELECT * FROM (
SELECT t.*,row_number() over(ORDER BY n_count DESC) r
FROM t_news t
ORDER BY t.n_count DESC
) t
WHERE r BETWEEN 4 AND 6
--通用語(yǔ)法: 解析函數(shù)() over(partition by 字段 order by 字段)
您可能感興趣的文章:- oracle,mysql,SqlServer三種數(shù)據(jù)庫(kù)的分頁(yè)查詢的實(shí)例
- Oracle實(shí)現(xiàn)分頁(yè)查詢的SQL語(yǔ)法匯總
- Oracle中使用Rownum分頁(yè)詳細(xì)例子
- oracle 分頁(yè) 很棒的sql語(yǔ)句
- 淺析Oracle和Mysql分頁(yè)的區(qū)別
- Oracle與Mysql主鍵、索引及分頁(yè)的區(qū)別小結(jié)
- Oracle、MySQL和SqlServe三種數(shù)據(jù)庫(kù)分頁(yè)查詢語(yǔ)句的區(qū)別介紹
- 分頁(yè)技術(shù)原理與實(shí)現(xiàn)之Java+Oracle代碼實(shí)現(xiàn)分頁(yè)(二)
- oracle分頁(yè)存儲(chǔ)過(guò)程 oracle存儲(chǔ)過(guò)程實(shí)例
- oracle實(shí)現(xiàn)一對(duì)多數(shù)據(jù)分頁(yè)查詢篩選示例代碼