我正试图在当天的每个小时获得一行,我打算将某些数据加入。
到目前为止,我只找到了这个解决方案,但我很确定这样做的方法比较简单..?
SElECT addtime('2016-01-22 00:00:00', t0.i) myHour
FROM (
SELECT '00:00:00' i
UNION SELECT '01:00:00'
UNION SELECT '02:00:00'
UNION SELECT '03:00:00'
UNION SELECT '04:00:00'
UNION SELECT '05:00:00'
UNION SELECT '06:00:00'
UNION SELECT '07:00:00'
UNION SELECT '08:00:00'
UNION SELECT '09:00:00'
UNION SELECT '10:00:00'
UNION SELECT '11:00:00'
UNION SELECT '12:00:00'
UNION SELECT '13:00:00'
UNION SELECT '14:00:00'
UNION SELECT '15:00:00'
UNION SELECT '16:00:00'
UNION SELECT '17:00:00'
UNION SELECT '18:00:00'
UNION SELECT '19:00:00'
UNION SELECT '20:00:00'
UNION SELECT '21:00:00'
UNION SELECT '22:00:00'
UNION SELECT '23:00:00'
) t0
答案 0 :(得分:3)
这只是一点点:
SELECT '2016-01-22 00:00:00' + INTERVAL (d0*10+d1) hour AS mydate
FROM (
SELECT 0 AS d0 UNION SELECT 1 UNION SELECT 2
) AS t1
cross JOIN (
SELECT 0 AS d1 UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION
SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9 ) AS t2
WHERE (d0*10+d1) < 24
order by d0,d1;
但是!!,如果你可以使用MariaDB,你可以使用序列引擎,并按照以下方式执行:
SELECT '2016-01-22 00:00:00' + INTERVAL seq HOUR as mydate from seq_0_to_23;
mydate
2016-01-22 00:00:00
2016-01-22 01:00:00
2016-01-22 02:00:00
2016-01-22 03:00:00
2016-01-22 04:00:00
2016-01-22 05:00:00
2016-01-22 06:00:00
2016-01-22 07:00:00
2016-01-22 08:00:00
2016-01-22 09:00:00
2016-01-22 10:00:00
2016-01-22 11:00:00
2016-01-22 12:00:00
2016-01-22 13:00:00
2016-01-22 14:00:00
2016-01-22 15:00:00
2016-01-22 16:00:00
2016-01-22 17:00:00
2016-01-22 18:00:00
2016-01-22 19:00:00
2016-01-22 20:00:00
2016-01-22 21:00:00
2016-01-22 22:00:00
2016-01-22 23:00:00
答案 1 :(得分:1)
这是我从前一段时间学到的一个技巧,唯一的问题是你应该引用一个超过24行的表,这应该很容易,因为你想在连接中使用它
select
addtime('2016-01-22 00:00:00', concat( @ num: =@ num + 1, ':00:00')) date
from
table,
(
select
@ num: =- 1
)
num limit 24