Created
November 26, 2019 20:42
-
-
Save w-mazed/12555961bb12b1d399a5541ca76be965 to your computer and use it in GitHub Desktop.
SQL: display months between two dates
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* List months between two dates, the example display months of current year up to now | |
*/ | |
SELECT DATE_FORMAT(m1, '%M %Y') AS `date` | |
FROM ( | |
SELECT ('2019-09-01' - INTERVAL DAYOFMONTH('2019-11-01')-1 DAY) + INTERVAL m MONTH AS m1 | |
FROM ( | |
SELECT @rownum:=@rownum+1 AS m FROM | |
(SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4) t1, | |
(SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4) t2, | |
(SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4) t3, | |
(SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4) t4, | |
(SELECT @rownum:=-1) t0 | |
) d1 | |
) d2 | |
WHERE m1<= NOW() | |
ORDER BY m1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment