MySQL:选择出现多次的行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4242990/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 17:46:34  来源:igfitidea点击:

MySQL: Select rows with more than one occurrence

mysql

提问by HyderA

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.idoccurs 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

回答by Stefan Mai

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

回答by Mohamed Saligh

Use DISTINCTfor avoiding multiple occurance

使用DISTINCT避免多次数