我有以下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
感谢。
答案 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