【问题标题】:FIltering SQL column by number of occurrences按出现次数过滤 SQL 列
【发布时间】:2017-08-19 00:31:50
【问题描述】:

这是对already asked and answered here.问题的后续处理

基本上,对于这样的表

Ford
Ford
Ford
Honda
Chevy
Honda
Honda
Chevy

如果需要出现次数的输出,如下所示:

Ford   3
Honda  3
Chevy  2

查询是:

select car_made, count(*) from cars
group by car_made

现在,我希望输出仅显示计数大于 2 的值。因此,所需的输出:

Ford   3
Honda  3

我该如何编写查询?

我试过了

select car_made, count(*) as carcount 
from cars
where carcount>2
group by car_made

但这似乎不起作用。

【问题讨论】:

  • 我假设您需要了解distinct 命令...
  • @Reborn: Group by 已经假定为distinct,除非指定了select all。但这有什么帮助呢?

标签: mysql


【解决方案1】:

你需要使用HAVING子句。

    select car_made, count(*) as carcount 
     from cars
    group by car_made
   having count(*) > 2

【讨论】:

  • 这是对的,您想要对组进行的任何过滤都需要使用 have 而不是 where。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-07-03
  • 2021-03-14
  • 2017-06-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多