好的,这有效:
SELECT DISTINCT(USER), COUNT(DISTINCT (USER)) AS numrows
FROM `sometable`
GROUP BY USER
ORDER BY USER asc
LIMIT 0, 20
我现在需要做的是将结果限制为数字> X.尝试使用嵌入式选择的许多解决方案,但没有运气。
答案 0 :(得分:2)
SELECT
DISTINCT(USER),
COUNT(DISTINCT (USER)) AS numrows
FROM
`sometable`
GROUP BY
USER
HAVING
COUNT(DISTINCT (USER)) > 10
ORDER BY
USER asc
LIMIT 0, 20
HAVING
与WHERE
类似,但适用于已分组的数据。您可以使用它来过滤掉一些不符合您要求的组。
答案 1 :(得分:1)
您可以省略DISTINCT
关键字,因为您已经对其进行了分组。要从聚合列中过滤掉结果,请使用HAVING
:
SELECT USER,
COUNT(DISTINCT USER) AS numrows
FROM sometable
GROUP BY USER
HAVING COUNT(DISTINCT USER) > ??? -- << define the value here
ORDER BY USER asc
LIMIT 0, 20
HAVING
和WHERE
正在过滤关键字,但区别在于HAVING
支持使用将在WHERE
无法过滤的情况下过滤的聚合函数。