I am trying to list all the months between two dates in Mysql and ended up with the following:
WITH RECURSIVE dates AS (
SELECT start_date as period, end_date
FROM progetti WHERE id = 1
UNION ALL
SELECT DATE_ADD(period, INTERVAL 1 MONTH), end_date FROM dates WHERE period < end_date )
SELECT * from dates;
That returns:
period | end date |
---|---|
2022-01-01 | 2022-12-31 |
2022-02-01 | 2022-12-31 |
2022-03-01 | 2022-12-31 |
2022-04-01 | 2022-12-31 |
2022-05-01 | 2022-12-31 |
2022-06-01 | 2022-12-31 |
2022-07-01 | 2022-12-31 |
2022-08-01 | 2022-12-31 |
2022-09-01 | 2022-12-31 |
2022-10-01 | 2022-12-31 |
2022-11-01 | 2022-12-31 |
2022-12-01 | 2022-12-31 |
2023-01-01 | 2022-12-31 |
Why do I also get the last row? isn’t 2023-01-01 greater than 2022-12-31?
The same happens with any set of dates. I always get one record more.
3
Answers
The last row is a selection from pre-last row where
2022-12-01
is less than2022-12-31
.If
day
component instart_date
may differ from01
then you must use complex query:This avoids
day
deviation (for example, ifstart_date = '2022-01-31
then all next rows will contain28
inday
component of the date).https://dbfiddle.uk/eViMoI9c
When its arrive to period
2022-12-01
the comparaisonWHERE period < end_date
will be translated to WHERE ‘2022-12-01’ < ‘2022-12-31’ which is true then as result you will get also2023-01-01
:To exclude
2023-01-01
try this :Demo here
Even though
2023-01-01
is greater than2022-12-31
, the conditionperiod < end_date
is still true since2023-01-01
is considered less than2022-12-31
for the purpose of the recursive query. This happens because you are comparing the individual months without considering the year.