cyborgv7
cyborgv7

Reputation: 63

PHP, PDO SQL query between two times then split per hour

I am trying to find out if it is possible to query between 2 hours and parse per hour EG

SELECT COUNT(*) FROM `production` WHERE `area` = '1' AND `datetime` BETWEEN '2016-10-21 08:00:00' AND "2016-10-21 10:00:00"


Result = 57

If it is possible i would like the output like this:

08:00 - 09:00 - 30
09:00 - 10:00 - 27

I am using PDO.

Thanks

Upvotes: 0

Views: 112

Answers (2)

jai dutt
jai dutt

Reputation: 790

You can try

 GROUP BY HOUR(datetime)

Upvotes: 1

cyborgv7
cyborgv7

Reputation: 63

I Found This Can Be Done At The SQL Level

SELECT HOUR(datetime), COUNT(*) FROM production
WHERE `area` = '1' AND `datetime` BETWEEN '2016-10-21 08:00:00' AND "2016-10-21 10:00:00"
GROUP BY HOUR(datetime)

This Returns

HOUR(datetime) - COUNT(*)
8 - 30
9 - 27
10 - 21

Upvotes: 1

Related Questions