-- row_number,不是rownum
--根据n_count从大到小排列,每页3条
SELECT ROWNUM r,t.* FROM t_news t
WHERE ROWNUM
ORDER BY t.n_count DESC
--问题:为什么order by以后,行号是乱的?
SELECT ROWNUM r,t.* FROM t_news t
--原因:先分配了行号,再根据n_count排序
--所以必须排序,再生成行号
SELECT ROWNUM r,t.* FROM (
SELECT t.* FROM t_news t ORDER BY t.n_count DESC ) t
--分页
--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页
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页
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开始!
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
|