MySQL:选择具有多个匹配项的行

时间:2022-12-08 03:35:25

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

这是我的查询。它从两个数据库中的两个表中选择一个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 != '' 

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

我只想显示en.id多次出现的行

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

2 个解决方案

#1


41  

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

#2


-5  

Use DISTINCT for avoiding multiple occurance

使用DISTINCT避免多次出现

#1


41  

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

#2


-5  

Use DISTINCT for avoiding multiple occurance

使用DISTINCT避免多次出现