Mysql order by hour

Asked

Viewed 102 times

0

In Mysql I have the following table:

2018-06-28 12:10:26
2018-06-25 11:10:26
2018-06-21 14:10:26
2018-06-31 00:10:26
2018-07-28 01:10:26
2018-07-29 09:10:26

Are the login days of each user (have the ID field but disregarded for my question), the customer wants a Dashboard that shows the amount of logins in each hour, I did so:

SELECT COUNT(id) AS total, dia FROM acesso GROUP BY HOUR(dia)

It worked fine, but what I’m not getting is to give an ORDER BY by the hour, for when show the result already come ordered, from 00:00 till 11:00, so:

00 hrs - 23 acessos
01 hrs - 14 acessos
02 hrs - 18 acessos
etc..

How to order? In my tests is appearing out of order.

  • What is being returned to you currently?

  • @Robertodecampos is out of order, even shows the access per hour, but not in the right order.

1 answer

0

I ended up getting it for whoever has the same doubt I do:

SELECT COUNT(id) AS total, dia FROM acesso
GROUP BY HOUR(dia)
ORDER BY HOUR(dia) ASC

is ORDER BY HOUR

Browser other questions tagged

You are not signed in. Login or sign up in order to post.