MySQL: How to group data per hour and get the latest hour

前端 未结 5 883
再見小時候
再見小時候 2020-12-31 16:15

I\'m trying to do a query that fetches data per hour but instead of the normal group by hour I want to narrow it down and only get the latest hour

5条回答
  •  滥情空心
    2020-12-31 16:25

    Do you mean one hour from NOW or latest full hour? If it's latest full hour something like this might work?

    SELECT userid, username, date_created
    FROM user_accounts 
    WHERE HOUR(date_created) = (SELECT HOUR(date_created) FROM user_accounts ORDER BY date_created DESC LIMIT 1);
    

    EDIT: Ahhh, now I think I get what you want... The last added entry on every given hour between your date range?

    If so then Codler's query is what you want.

提交回复
热议问题