I have a field createdTime in a table users with current time stamp as data type. I am trying to display the count of users in every minute in each day . By using the php loop and select query its little bit slow. How can i do this using mysql query ? Is there any method to select the values in a minute using a single query ? My aim is to display the details as
Thanks for your quick reply…
I tried your query like
SELECT DATE_FORMAT( time, ‘%Y-%m-%d %H:%i’ ) , COUNT( * ) AS userCount
FROM uesr
WHERE time
BETWEEN ‘2012-08-22 00:00:00’
AND ‘2012-08-22 23:59:59’
GROUP BY DATE_FORMAT( time, ‘%Y-%m-%d %H:%i’ )
Its working correctly…
But if a situation like no user at the time 2012-08-22 00:05:00, Can i display this value as 0 using the query ?
Currently the above query does not consider the ‘2012-08-22 00:05:00’ because no entry for this time value…
Actually i am expecting 60*24 rows for every hours ,ie if there is no users for particular minute i would like to display that field value as 0
Is it possible with the mysql query ?
The only way I know to “fill the gaps” as it were is to create a table with all possible dates you will query (possibly generated when needed) and then LEFT JOIN against that. You could create such a table for several weeks/months/years in advance to speed things up if you like.
I’m not sure on the performance impact of such a table / handing it in your code. Handling in the code is more sound, but the table with dates might be faster depending how many dates you put in there etc.
So basically you’d just a create a table like this