Skip to content

Instantly share code, notes, and snippets.

@w-mazed
Last active November 26, 2019 20:44
Show Gist options
  • Save w-mazed/bc79112e7e658ba10bcbf1d54cf431d2 to your computer and use it in GitHub Desktop.
Save w-mazed/bc79112e7e658ba10bcbf1d54cf431d2 to your computer and use it in GitHub Desktop.
SQL: display days between two dates
/**
* List days between two dates, the example display days of current month up to now
*/
SELECT `date`
FROM (
SELECT adddate('1970-01-01', t4*10000 + t3*1000 + t2*100 + t1*10 + t0) `date`
FROM (
SELECT 0 t0 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
) t0, (
SELECT 0 t1 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
) t1, (
SELECT 0 t2 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
) t2, (
SELECT 0 t3 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
) t3, (
SELECT 0 t4 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
) t4
) v
WHERE `date` BETWEEN DATE_FORMAT(NOW(), '%Y-%m-01') AND NOW()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment