Click here to Skip to main content
15,886,873 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi.,

i have a table in MySQL having 3 columns called Name,Number,DateInserted. In DateInserted column i m inserting the date and time. now i want to get the records inserted by every hour means in the following format

FromHour - ToHour - counts
11 - 12 - 358

10 - 11 - 55

9 - 10 - 112

8 - 9 - 15

7 - 8 - 856

6 - 7 - 558

5 - 6 - 556

4 - 5 - 458

3 - 4 - 741

2 - 3 - 125

1 - 2 - 789

0 - 1 - 856


thanks to ALL..

..
Posted

here is a HOUR[^] function in MySQL. You can use it as GROUP field, like this:
SQL
select HOUR(DateInserted) as FromHour, count(*) from tabelname where ...
group by HOUR(DateInserted)

As HOUR will give you 1 from 1:00 till 1:59, you yous have to add 1 to get ToHour.
 
Share this answer
 
v2
Comments
Mohibur Rashid 27-Sep-12 4:26am    
This one is better than mine
This is an example for you, rest is upto you

SQL
create table def(d DATETIME);
    -> insert into def values
    -> ('2012-01-02 12:10:11'),
    -> ('2012-01-02 12:12:11'),
    -> ('2012-01-02 01:12:11'),
    -> ('2012-01-02 01:32:11');
SELECT COUNT(*) FROM def GROUP BY MID(d, 1, 13);


Find out rest by yourself
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900