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
MySQL: Select rows with more than one occurrence
提问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.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
回答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 DISTINCT
for avoiding multiple occurance
使用DISTINCT
避免多次数