【发布时间】:2019-06-11 08:52:04
【问题描述】:
我正在尝试获取状态大于 1 的所有行。
我的桌子:
user_id|state
--------------
1000000|Active
1000000|Created
1000001|Active
1000000|Deleted
1000002|Active
1000001|Created
1000003|Active
我的查询:
select user_id, count(state) from docs group by user_id order by count(state) desc;
结果是:
user_id | count(state)
1000000 | 3
1000001 | 2
1000002 | 1
1000003 | 1
但我只需要打印 count(state) 大于 1 的值
我正在尝试这个,但它不起作用:
select user_id, count(state) from docs where count(state) > 1 group by user_id;
【问题讨论】:
-
WHERE限制SELECT显示的行数,HAVING限制GROUP BY显示的组数,所以需要have而不是where
标签: sql group-by where-clause