如何在MySQL中选择过去30天的日期?

时间:2014-01-19 17:52:24

标签: mysql date select nested-queries

我可以在MySQL以某种方式列出过去30天的日期吗?不是来自桌子!

例如,我想是这样的:

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

这可能吗?

2 个答案:

答案 0 :(得分:7)

我从其他人的代码中一起攻击了这个,但似乎有效:

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

valex的原始代码在这里:

How to get a list of months between two dates in mysql

答案 1 :(得分:0)

你可以用“明确”的方式做到这一点。也就是说,生成一系列数字并计算日期:

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