Rownum 응용 (페이징 처리)

양혜정·2024년 3월 5일

Oracle

목록 보기
25/49

Rownum 응용

페이징처리의 공식

where RNO between 
(조회하고자하는페이지번호 * 한페이지당보여줄행의개수) 
						- (한페이지당보여줄행의개수 - 1) 
and (조회하고자하는페이지번호 * 한페이지당보여줄행의개수);
   => 한 페이지당 3개씩 보여주고자 한다.
   1 페이지 ==> rownum : 1 ~ 3    boardno : 14 ~ 12
   2 페이지 ==> rownum : 4 ~ 6    boardno : 11 ~ 9
   3 페이지 ==> rownum : 7 ~ 9    boardno :  8 ~ 6
   4 페이지 ==> rownum : 10 ~ 12  boardno :  5 ~ 3
   5 페이지 ==> rownum : 13 ~ 15  boardno :  2 ~ 1

페이지당 3개씩 보여주기

-- [1페이지]
SELECT rno as 번호
     , boardno as 글번호
     , subject as 글제목
     , userid as 글쓴이
     , registerday as 작성일자
FROM
(
     select rownum as rno
          , boardno, subject, userid, registerday 
     from
     (
         select boardno, subject, userid 
              , to_char(registerday,'yyyy-mm-dd 
              					hh24:mi:ss') as registerday
         from tbl_board
         order by 1 desc
     ) V
) T
WHERE rno between 1 and 3;  -- 1페이지
/*
=== 페이징처리의 공식 ===
where RNO between ( 1 * 3 ) - ( 3 - 1 ) and ( 1 * 3 )
where RNO between 1 and 3
*/

-- [2페이지]
SELECT rno as 번호
     , boardno as 글번호
     , subject as 글제목
     , userid as 글쓴이
     , registerday as 작성일자
FROM
(
     select row_number() over(order by boardno desc) as rno 
          , boardno, subject, userid 
          , to_char(registerday,'yyyy-mm-dd hh24:mi:ss') 
          									as registerday
     from tbl_board
     order by 2 desc
) V
WHERE rno between 4 and 6;  -- 2페이지
/*
=== 페이징처리의 공식 ===
where RNO between ( 2 * 3 ) - ( 3 - 1 ) and ( 2 * 3 )
where RNO between 4 and 6
*/
...

참고

rownum : https://velog.io/@jjoung-2j/Pseudo%EC%9D%98%EC%82%AC-%EC%9C%A0%EC%82%AC-%EB%AA%A8%EC%A1%B0-Column


정리

-> local_hr에서작업한것

0개의 댓글