개발모음집

조회할 때 row number 조회하는 방법 본문

DB/RDBMS

조회할 때 row number 조회하는 방법

void 2021. 1. 5. 10:00

select할 때 row들을 그룹핑해서 중복되는 row가 많은 순으로 ranking 조회하고 싶었다.

set @row_num = 0;

SELECT @row_num:=@row_num + 1 as ranking, count(C) as count FROM a GROUP BY b HAVING count > 1;

이렇게 하니까

조회할 때마다 row_num의 수가 올라갔다. 그래서 select할 때마다 set 0으로 해주면 될 것 같긴한데 디비가 바뀌거나 새

플랫폼을 할 때마다 기억해야할 것 같아서 실수할 것 같다.

출처 : clausvisby.com/ko/1737-mysql-how-to-get-row-number-order.html

그래서 select할 때마다 row_num을 0으로 초기화해주는 식으로 했다

SELECT @row_num:=@row_num + 1 as ranking, sk.c, count(sk.c) as count FROM a sk , 
(SELECT @row_num:=0) r GROUP BY b HAVING count >1;

 

출처: its-easy.tistory.com/18

'DB > RDBMS' 카테고리의 다른 글

crontap으로 mysql dump 하기  (0) 2021.02.17
mysql event_schedule query  (0) 2020.12.23
Setting event_scheduler to on in mysql5.7  (0) 2020.12.22
MYSQL Left Join COUNTS from multiple tables  (0) 2020.12.14
crontab 설정 파일  (0) 2020.09.30