Skip to content
Advertisement

MySQL One row for each hour 00:01:00 and 23:59:00 interval of the day

I’m trying to get one row for each hour of the day, which I intent to left join certain data to.

I need selected the 00:01:00 and 23:59:00 interval hour for each day.

I have tried this solution but I can’t selected 00:01:00 and 23:59:00 interval hour but only 00:00:00 and 23:00:00 interval hour.

How to do resolve this ?

Can you help me please ?

Thank you in advance or any help.

Edit 01

I need this result :

Advertisement

Answer

You seem to be trying to generate a hours table. If so, I would recommend outputing two columns: one for the lower bound of the interval (which will be inclusive), the other for the upper bound (which should be exclusive)

This produces a resultset like:

start_date          | end_date           
:------------------ | :------------------
2020-03-10 00:00:00 | 2020-03-10 01:00:00
2020-03-10 01:00:00 | 2020-03-10 02:00:00
2020-03-10 02:00:00 | 2020-03-10 03:00:00
...
2020-03-10 21:00:00 | 2020-03-10 22:00:00
2020-03-10 22:00:00 | 2020-03-10 23:00:00
2020-03-10 23:00:00 | 2020-03-11 00:00:00

You can then join that with your original table with half-open interval, like so:

The upside of this approach is that it properly handles the first and last minute of each interval.


Edit: to generate your exact desired results, you can do:

Demo on DB Fiddle

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement