开发者

Is there a way to limit the result AFTER the grouping has taken place?

开发者 https://www.devze.com 2023-03-19 10:22 出处:网络
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 appe

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消