MYSQL 질문 드립니다.. 채택완료
select wr_id, mb_no, count(*) from g5_write.... group by wr_id, mb_no;
| wr_id | mb_no | count(*) |
| 18 | 7 | 3 |
| 19 | 9 | 5 |
| 20 | 9 | 1 |
위와 같은 결과가 나왔을 때,
mb_no가 중복이 있는 경우 (9가 2개) count(*)가 최대인 행만 남기고 싶습니다.
wr_id 가 18,19인 행만 남게요.
어떻게 쿼리를 짜야할까요?..
답변 4개
채택된 답변
+20 포인트
답변에 대한 댓글 3개
�
3년 전
mysql gruop by max 라고 구글링해보시면 비슷한 내용이 많이 나오는데 참고해보시면 될 것 같아요!
http://jason-heo.github.io/mysql/2014/03/05/find-max-row.html
https://helloino.tistory.com/120
https://bamdule.tistory.com/219
http://jason-heo.github.io/mysql/2014/03/05/find-max-row.html
https://helloino.tistory.com/120
https://bamdule.tistory.com/219
�
유기농감자
3년 전
해결했습니다! 5건만 조회하면 돼서 성능은 어떨지 모르겠지만..
SELECT t1.*
FROM (
SELECT wr_id, mb_no, count(*) as cnt
FROM g5_write_...
GROUP BY wr_id, mb_no
) t1
INNER JOIN
(
select mb_no,max(cnt) as max from (
SELECT wr_id, mb_no, count(*) as cnt
FROM g5_write_...
GROUP BY wr_id, mb_no
) c
GROUP BY mb_no
) t2
ON t1.mb_no = t2.mb_no AND t1.cnt = t2.max;
;
감사합니다!
SELECT t1.*
FROM (
SELECT wr_id, mb_no, count(*) as cnt
FROM g5_write_...
GROUP BY wr_id, mb_no
) t1
INNER JOIN
(
select mb_no,max(cnt) as max from (
SELECT wr_id, mb_no, count(*) as cnt
FROM g5_write_...
GROUP BY wr_id, mb_no
) c
GROUP BY mb_no
) t2
ON t1.mb_no = t2.mb_no AND t1.cnt = t2.max;
;
감사합니다!
댓글을 작성하려면 로그인이 필요합니다.
넓은마인드
3년 전
Copy
select wr_id, mb_no, count(*) from g5_write.... group by wr_id, mb_no order by count(*) desc limit 0,1;
정렬을 count(*) desc 하시고 limit 0,1 해서 최상위 한개만 가져오심 됩니다
로그인 후 평가할 수 있습니다
댓글을 작성하려면 로그인이 필요합니다.
3년 전
이걸 사용하시면 됩니다.
로그인 후 평가할 수 있습니다
댓글을 작성하려면 로그인이 필요합니다.
댓글을 작성하려면 로그인이 필요합니다.
답변을 작성하려면 로그인이 필요합니다.
로그인