MySQL 按 COUNT(*) 过滤?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5664103/
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
Filter by COUNT(*)?
提问by DonutReply
Is it possible to group results and then filter by how many rows are in the group?
是否可以对结果进行分组,然后按组中的行数进行过滤?
Something like this:
像这样的东西:
SELECT * FROM mytable WHERE COUNT(*) > 1 GROUP BY name
回答by Joe Stefanelli
You want to use HAVING to filter on the aggregate function.
您想使用 HAVING 来过滤聚合函数。
SELECT name, COUNT(*)
FROM mytable
GROUP BY name
HAVING COUNT(*) > 1
回答by JohnP
You need to use HAVING
你需要使用 HAVING
SELECT * FROM mytable GROUP BY name HAVING COUNT(*) > 1
Although, SELECT *
doesn't make much sense when you're grouping. I assume it's just for an example
虽然,SELECT *
当你分组时没有多大意义。我认为这只是一个例子
回答by R Hill
You want a HAVING clause.
你想要一个 HAVING 子句。
SELECT *
FROM mytable
GROUP BY name
HAVING COUNT(*) > 1
回答by R Hill
Use having in your query:
在您的查询中使用 have:
SELECT * FROM mytable GROUP BY name having COUNT(*) > 1