MySQL – Select date from a range without table

October 19, 2016 17:52
Tags: ,

Sometimes you want to get the date range from MySQL without table. Here you are:

SELECT '2016-09-01' + INTERVAL a + b DAY AS date_selected
FROM
(SELECT 0 a UNION SELECT 1 a UNION SELECT 2 UNION SELECT 3
UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7
UNION SELECT 8 UNION SELECT 9 ) d,
(SELECT 0 b UNION SELECT 10 UNION SELECT 20
UNION SELECT 30 UNION SELECT 40) m
WHERE '2016-09-01' + INTERVAL a + b DAY < '2016-10-01'
ORDER BY a + b

It is useful when you want to generate some reports.

Share

Comments are closed.