MySQL: Select rows with more than one occurrence

2020-02-21 05:48发布

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

标签: mysql
2条回答
贪生不怕死
2楼-- · 2020-02-21 06:26

Use DISTINCT for avoiding multiple occurance

查看更多
够拽才男人
3楼-- · 2020-02-21 06:31
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
查看更多
登录 后发表回答