好的,这有效:
SELECT DISTINCT(USER), COUNT(DISTINCT (USER)) AS numrows
FROM `sometable`
GROUP BY USER
ORDER BY USER asc
LIMIT 0, 20
我现在需要做的是将结果限制为 numrows > X。使用嵌入式选择尝试了许多解决方案,但没有运气。
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
,但应用于已经分组的数据。您可以使用它来过滤掉一些不符合您要求的组。
您可以省略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
。