I am trying to group a steam of posts by hour, 开发者_C百科so only 1 post shows per hour but I think what is happening is that mysql limits the results then groups them and that leads to posts to appear under an hour.
Is there a way to apply the limit after the grouping as taking place?
Sample:
SELECT * FROM posts
GROUP BY posts.userid, FLOOR((UNIX_TIMESTAMP()- UNIX_TIMESTAMP(posts .time))/3600) #Hourly limiter
LIMIT 20
I want this query to do the grouping before the limiting so I can get accurate results.
Generally a HAVING clause is used to filter groups, as the HAVING clause is evaluated on the results of the GROUP BY. See http://www.electrictoolbox.com/mysql-group-by-having/ for examples.
精彩评论