我如何使用mysql获取最近10天的记录

时间:2018-08-17 14:37:45

标签: mysql

我想检索最近10天记录中的所有数据,但是当我添加“ where”时,它无法正常工作。

我的表结构:

CREATE TABLE `vip` (
  `id` int(11) NOT NULL auto_increment,
  `cancel_at_period_end` datetime NULL,
  `creation_date` datetime default NULL,
 `answer` varchar(5) collate utf8_unicode_ci default NULL,
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=499 ;

我使用了类似的查询:

SELECT days.day, count(users_vip.id) count
FROM (SELECT curdate() as day 
                          union select curdate() - interval 1 day
                          union select curdate() - interval 2 day
                          union select curdate() - interval 3 day
                          union select curdate() - interval 4 day
                          union select curdate() - interval 5 day
                          union select curdate() - interval 6 day
                          union select curdate() - interval 7 day
                          union select curdate() - interval 8 day
                          union select curdate() - interval 9 day) days
LEFT JOIN users_vip on days.day = CONVERT(users_vip.creation_date, date)
WHERE users_vip.current_period_end_date >= NOW()
GROUP BY days.day

输出为:

date          count
2018-08-15    1

我想要的结果应该是这样的:

date          count
2018-08-08    0
2018-08-09    0
2018-08-10    0
2018-08-11    0
2018-08-12    0
2018-08-13    0
2018-08-14    0
2018-08-15    1
2018-08-16    0
2018-08-17    0

更新:

SELECT days.day, count(users_vip.id) count
FROM (SELECT curdate() as day 
                          union select curdate() - interval 1 day
                          union select curdate() - interval 2 day
                          union select curdate() - interval 3 day
                          union select curdate() - interval 4 day
                          union select curdate() - interval 5 day
                          union select curdate() - interval 6 day
                          union select curdate() - interval 7 day
                          union select curdate() - interval 8 day
                          union select curdate() - interval 9 day) days
LEFT JOIN users_vip on days.day = CONVERT(users_vip.creation_date, date) AND users_vip.current_period_end_date >= NOW()
GROUP BY days.day

2 个答案:

答案 0 :(得分:0)

我认为您应该将日历表加入一个子查询,该子查询按天汇总计数:

Int64

答案 1 :(得分:0)

我们可以将WHERE更改为AND,以将谓词从WHERE子句重新定位到ON子句。

我也避免使用creation_date周围的函数包装器,以允许使用索引。我要重写匹配条件。

我会做这样的事情:

SELECT days.day
     , COUNT(users_vip.id)     AS count
  FROM ( SELECT CURDATE() AS day 
         UNION ALL SELECT CURATE() + INTERVAL -1 DAY
         UNION ALL SELECT CURATE() + INTERVAL -2 DAY
         UNION ALL SELECT CURATE() + INTERVAL -3 DAY
         UNION ALL SELECT CURATE() + INTERVAL -4 DAY
         UNION ALL SELECT CURATE() + INTERVAL -5 DAY
         UNION ALL SELECT CURATE() + INTERVAL -6 DAY
         UNION ALL SELECT CURATE() + INTERVAL -7 DAY
         UNION ALL SELECT CURATE() + INTERVAL -8 DAY
         UNION ALL SELECT CURATE() + INTERVAL -9 DAY
       ) days
  LEFT
  JOIN users_vip
    ON users_vip.creation_date            >= days.day 
   AND users_vip.creation_date            <  days.day + INTERVAL 1 DAY
   AND users_vip.current_period_end_date  >= NOW() 
 GROUP
    BY days.day