views:

189

answers:

1

I have a table containing view/click records. The time is stored in a unix timestamp and I need to be able to pull out all of them within the specific month/day (based off of timestamps), but more importantly and the part I don't know how to do is group them by hour. I need to be able to do this in a single query rather than looping through each hour.

Database is MySQL, language is PHP.

+1  A: 
select hour(somedate), count(*) from yourtable group by hour(somedate)

If you need all three:

select month(somedate), day(somedate), hour(somedate), count(*) from yourtable group by month(somedate), day(somedate), hour(somedate)
ʞɔıu
When I run the following query:SELECT * , HOUR( TIMESTAMP ) AS HOUR FROM ad_viewLIMIT 0 , 30It returns 838 as the hour, where it should be the number of hours in the current day.
Webnet
Sorry, the number of hours into the current day (0-24) that the timestamp is.
Webnet
are you sure that's really a timestamp? is it a timestamp integer stored in an int column or something?
ʞɔıu
hour() takes a time, not a unix timestamp. You want to use hour(from_unixtime(somedate)). Same for your other functions?
Keith Randall
if it's a unix timestamp int rather than a column of type timestamp, keith may be right
ʞɔıu
You had it right, I used this:[code]SELECT COUNT(id) as view_count, DAY(FROM_UNIXTIME(timestamp)) as day FROM ad_view WHERE timestamp > '.$start.' AND timestamp < '.$end.' GROUP BY day[/code]
Webnet