HyderA
HyderA

Reputation: 21401

MySQL: Select rows with more than one occurrence

Here's my query. It selects a list of ids from two tables across two databases. The query works fine.

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 != '' 

I only want to display rows where a en.id occurs more than once

So for instance, if this was the current query result

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

I only want to query to show this instead

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

Upvotes: 32

Views: 78288

Answers (1)

Stefan Mai
Stefan Mai

Reputation: 23959

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

Upvotes: 71

Related Questions