본문 바로가기

MySQL

MySQL - rownum 구현

8.0.26 버전 mysql입니다. 

select a.player_id,a.player_name,a.back_no 
from (select PLAYER_NAME,PLAYER_ID,BACK_NO,row_number() over() num from player) a 
where rownum < 10;

을 실행하면 rownum이 없다고 메세지가 뜹니다. 

sql 가이드북에서는 (sql 자격증책) rownum sudo column이라고 시스템적으로 제공된다고 적혀있습니다. 
그러나
select ename,sal
from emp
where rownum < 4
order by sal desc; 실행 시 rownum이 칼럼을 정의하지 않았다고 메시지가 뜨게 됩니다. (책 예제)

해결방법은 서브쿼리를 이용하였는데요, row_number() 함수를 이용했습니다.

select a.player_id,a.player_name,a.back_no 
from (select PLAYER_NAME,PLAYER_ID,BACK_NO,row_number() over() num from player) a 
where num < 10;

select 절에서 정의해서 이용할 수 있지 않을까라고 생각했었습니다. 그러나 테이블 자체에 없는 데이터여서 칼럼으로 인식을 하지 못했습니다. 서브 쿼리로 정의 후 where절에서 이용하면 구현할 수 있습니다.

반응형

'MySQL' 카테고리의 다른 글

MySQL - ratio_to_report 구현  (0) 2021.09.26
MySQL - rollup 사용  (0) 2021.09.25
MySQL - index, view, group by  (0) 2021.08.19
MySQL - join, union, sub-query  (0) 2021.08.18
MySQL - foreign key  (0) 2021.08.14