MySQL:返回COUNT(*)大于的字段

时间:2010-10-18 14:26:43

标签: database mysql

我有以下SQL,但我只想返回'hits'大于10的行。

SELECT clicks.affiliate, COUNT(*) AS hits, affiliates.title, affiliates.url
FROM clicks
INNER JOIN affiliates ON affiliates.id = clicks.affiliate
GROUP BY clicks.affiliate

感谢。

3 个答案:

答案 0 :(得分:20)

要按聚合过滤,您需要使用having子句。与许多RDBMS不同,MySQL确实允许您在此上下文中使用列别名(大多数其他RDBMS也会坚持将affiliates.title, affiliates.url添加到group by子句中

SELECT clicks.affiliate, COUNT(*) AS hits, affiliates.title, affiliates.url
FROM clicks
INNER JOIN affiliates ON affiliates.id = clicks.affiliate
GROUP BY clicks.affiliate
HAVING hits > 10

答案 1 :(得分:3)

SELECT clicks.affiliate, COUNT(*) AS hits, affiliates.title, affiliates.url
FROM clicks
INNER JOIN affiliates ON affiliates.id = clicks.affiliate
GROUP BY clicks.affiliate
HAVING COUNT(*) > 10

答案 2 :(得分:0)

 ...
HAVING hits > 10