본문 바로가기
MySql

MySQL: Select rows with more than one occurrence

by 베이스 공부 2020. 10. 28.
반응형

여기 내 질문이 있습니다. 두 데이터베이스의 두 테이블에서 ID 목록을 선택합니다. 쿼리가 제대로 작동합니다.

select en.id, fp.blogid
from french.blog_pics fp, french.blog_news fn, english.blog_news en 
where fp.blogid = fn.id 
and en.title_fr = fn.title 
and fp.title != '' 

en.id 가 두 번 이상 발생하는 행만 표시하고 싶습니다.

예를 들어 이것이 현재 쿼리 결과라면

en.id fp.blogid
---------------
  10     12
  12     8
  17     9
  12     8

대신 이것을 표시하기 위해 쿼리하고 싶습니다.

 en.id fp.blogid occurrences
 -----------------------------
  12     8           2

 

해결 방법

 

select en.id, fp.blogid, count(*) as occurrences
from french.blog_pics fp, french.blog_news fn, english.blog_news en 
where fp.blogid = fn.id 
and en.title_fr = fn.title 
and fp.title != ''
group by en.id
having count(*) > 1

 

참조 페이지 https://stackoverflow.com/questions/4242990

 

 

반응형

댓글