我有以下SQL,但我只想返回“匹配”大于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

谢谢。

最佳答案

要按汇总过滤,您需要使用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

关于database - MySQL:返回COUNT(*)大于的字段,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/3960154/

10-11 02:08