0

I have a mysql table where each entry contains among other, a location string and a datetime field.

I would like to build a mysql query that generates a report with the count(items) grouped by hour of day (on the rows) and the locations (on the columns).

So far I'm manually generating reports grouped by location and I have a WHERE clause where I iterate over each hour of day (24 in number). This is too cumbersome and I would like to do it automatically.

EDIT

This is the query i'm now trying to use based on the answer from fxzuz

SELECT locations.locationName, DATE_FORMAT(  '%Y-%m-%d %H', entries.CreatedOn ) , COUNT( pk_entry ) 
FROM entries
LEFT JOIN locations ON locations.computerName = entries.location
GROUP BY DATE_FORMAT(  '%Y-%m-%d %H', entries.CreatedOn ) 
4

1 回答 1

2

mysql 函数 DATE_FORMAT 怎么样?

SELECT location, DATE_FORMAT(datetime, '%Y-%m-%d %H'), COUNT(id) FROM table GROUP BY DATE_FORMAT('%Y-%m-%d %H', datetime);
于 2012-06-18T21:19:20.357 回答