How to choose the last 30 days in MySQL?

Can I somehow indicate the dates of the last 30 days in MySQL? Not from the table!

For example, I think something like this:

SELECT date WHERE date BETWEEN SUBDATE(NOW(), INTERVAL 30 DAY) AND NOW();

Is it possible?

+4
source share
2 answers

I cracked this along with other code, but it works:

SELECT DATE_FORMAT(m1, '%d %b %Y')
FROM (
SELECT SUBDATE( NOW() , INTERVAL 30 DAY) + INTERVAL m DAY 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

The source code for valex is here:

How to get list of months between two dates in mysql

+7
source

You can do this in an "explicit" way. That is, generate a series of numbers and calculate the date:

select date(date_sub(now(), interval n.n day) as thedate
from (select 1 as n union all
      select 2 union all
      . . .
      select 30
     ) n
0
source

All Articles