The question was edited to clarify there are no tables.
MySQL (to my knowledge) has no sequence generator, and in order to prevent a procedure here's a trick you can use to generate a list of dates.
SELECT DATE(NOW() - INTERVAL n DAY) AS date,
DAYNAME(NOW() - INTERVAL n DAY) AS day
FROM (
SELECT 0 n
UNION SELECT 1
UNION SELECT 2
UNION SELECT 3
UNION SELECT 4
UNION SELECT 5
UNION SELECT 6
) q
ORDER BY day ASC
;
Note that this is a very cumbersome approach, imagine what a range of more dates would look like. If you need this for a one-shot operation, it'll be fine, if you are going to use this on a regular basis, you may want to create a procedure for it.
The table based answer (perhaps useful to others)
MySQL supports the DATE_ADD
and DATE_SUB
functions, which allows you to subtract an INTERVAL
. Internally this is optimised to the following syntax, which you may find more flexible
SELECT *
FROM table
WHERE field >= NOW() - INTERVAL 7 DAY;
Most 'time units' are supported (MINUTE
, HOUR
, WEEK
, etc) and you can replace the NOW()
part with a field name too.
The DATE_ADD
manual has some examples on how to use INTERVAL
.
Basic syntax:
date + INTERVAL expr unit
date - INTERVAL expo unit
And implementation examples:
mysql> SELECT '2008-12-31 23:59:59' + INTERVAL 1 SECOND;
-> '2009-01-01 00:00:00'
mysql> SELECT INTERVAL 1 DAY + '2008-12-31';
-> '2009-01-01'
mysql> SELECT '2005-01-01' - INTERVAL 1 SECOND;
-> '2004-12-31 23:59:59'