【问题标题】:How to select last 30 days dates in MySQL?如何在 MySQL 中选择最近 30 天的日期?
【发布时间】:2014-02-08 19:41:18
【问题描述】:

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

例如我这样想:

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

这可能吗?

【问题讨论】:

标签: mysql date select nested-queries


【解决方案1】:

我从别人的代码中破解了这个,但它似乎有效:

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

【讨论】:

    【解决方案2】:

    您可以通过“显式”方式执行此操作。即生成一系列数字并计算日期:

    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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多