반응형
[MySQL]에서 Oracle처럼 ROWNUM을 사용하여 번호매기기
1) SET 구문을 사용하여 ROWNUM 값을 초기화 후 조회
SET @rownum:=0;
SELECT
@rownum:=@rownum+1 AS `NUM`, tbl.*
FROM lecture.idol_member tbl;
2) WHERE 절에서 초기화
SELECT
@rownum:=@rownum+1 AS `NUM`,
tbl.*
FROM lecture.idol_member tbl
--WHERE (@rownum:=0)=0; 초기화 하지 않은 경우
SELECT
@rownum:=@rownum+1 AS `NUM`,
tbl.*
FROM lecture.idol_member tbl
WHERE (@rownum:=0)=0;
3) 페이징(Paging) 처리(행 제한하기)
SET @rownum:=0;
SELECT
@rownum:=@rownum+1 AS `NUM`,
tbl.*
FROM lecture.idol_member tbl
limit 0, 5;
SET @rownum:=5;
SELECT
@rownum:=@rownum+1 AS `NUM`,
tbl.*
FROM lecture.idol_member tbl
limit 5, 5;
SET @rownum:=10;
SELECT
@rownum:=@rownum+1 AS `NUM`,
tbl.*
FROM lecture.idol_member tbl
limit 10, 5;
SET @rownum:=15;
SELECT
@rownum:=@rownum+1 AS `NUM`,
tbl.*
FROM lecture.idol_member tbl
limit 15, 5;
SET @rownum:=20;
SELECT
@rownum:=@rownum+1 AS `NUM`,
tbl.*
FROM lecture.idol_member tbl
limit 20, 5;
SET @rownum:=25;
SELECT
@rownum:=@rownum+1 AS `NUM`,
tbl.*
FROM lecture.idol_member tbl
limit 25, 5;
4) 실습
Set @NUM :=0;
SELECT @NUM:=@NUM+1 AS `NO`, T.* FROM `MEMBER` T LIMIT 0,5;
Set @NUM :=5;
SELECT @NUM:=@NUM+1 AS `NO`, T.* FROM `MEMBER` T LIMIT 5,5;
Set @NUM :=10;
SELECT @NUM:=@NUM+1 AS `NO`, T.* FROM `MEMBER` T LIMIT 10,5;
Set @NUM :=15;
SELECT @NUM:=@NUM+1 AS `NO`, T.* FROM `MEMBER` T LIMIT 15,5;
Set @NUM :=20;
SELECT @NUM:=@NUM+1 AS `NO`, T.* FROM `MEMBER` T LIMIT 20,5;
Set @NUM :=25;
SELECT @NUM:=@NUM+1 AS `NO`, T.* FROM `MEMBER` T LIMIT 5,5;
Set @NUM :=30;
SELECT @NUM:=@NUM+1 AS `NO`, T.* FROM `MEMBER` T LIMIT 30,5;
반응형
'🛢 3. Database > 3-1 Mysql' 카테고리의 다른 글
[Database] 14. SELECT 구절 순서와 처리 과정 (1) | 2021.09.28 |
---|---|
[Database] 13. DISTINCT와 COUNT, SQL 함수(문자열, 숫자, 날짜, 변환, NULL 관련) (0) | 2021.09.27 |
[Database] 11. 정규표현식 (0) | 2021.09.23 |
[Database] 10. 연산을 통한 데이터 조회 (0) | 2021.09.17 |
[Database] 09. 트랜잭션 처리 - COMMIT 과 ROLLBACK (0) | 2021.09.16 |
댓글