MySQL在日期范围内每天计算活动记录

时间:2017-02-05 05:34:40

标签: mysql

我有一个MySQL表,用于具有这些重要列的产品。

-id

-arrived

-released

使用已到达和已发布的列,我想循环一下这个月的日子,比如说2017-01-01'通过' 2017-01-30'并检查有多少产品"活跃"。这意味着它实质上会循环每天的日期范围,并检查有多少产品活跃于2017-01-01'以及日期范围内的每一天等等。

我用来检查个别日期的代码,例如2017-01-01'如下。

SELECT COUNT(id)
FROM products_booking
WHERE (released IS NULL OR released >= '2017-01-01') AND (arrived <= '2017-01-01')

这将返回&#34;活跃&#34;这一天的产品。

但是,我希望在指定的日期范围内执行此操作,以便能够计算&#34;活动&#34;的数量。指定日期范围内每天的产品。

我尝试过以下无效,似乎我用这种方法走错了路。

SELECT MonthDate.Date, COALESCE(COUNT(IB.id), 0) AS Total
FROM (
    SELECT 1 AS Date UNION ALL
    SELECT 2 UNION ALL
    SELECT 3 UNION ALL
    SELECT 4 UNION ALL
    SELECT 5 UNION ALL
    SELECT 6 UNION ALL
    SELECT 7 UNION ALL
    SELECT 8 UNION ALL
    SELECT 9 UNION ALL
    SELECT 10 UNION ALL
    SELECT 11 UNION ALL
    SELECT 12 UNION ALL
    SELECT 13 UNION ALL
    SELECT 14 UNION ALL
    SELECT 15 UNION ALL
    SELECT 16 UNION ALL
    SELECT 17 UNION ALL
    SELECT 18 UNION ALL
    SELECT 19 UNION ALL
    SELECT 20 UNION ALL
    SELECT 21 UNION ALL
    SELECT 22 UNION ALL
    SELECT 23 UNION ALL
    SELECT 24 UNION ALL
    SELECT 25 UNION ALL
    SELECT 26 UNION ALL
    SELECT 27 UNION ALL
    SELECT 28 UNION ALL
    SELECT 29 UNION ALL
    SELECT 30 UNION ALL
    SELECT 31) AS MonthDate
LEFT JOIN products AS IB
ON MonthDate.Date >= DAY(IB.released) AND MonthDate.Date <= DAY(IB.arrived)
WHERE MonthDate.Date <= DAY(LAST_DAY('2017-01-30'))
GROUP BY MonthDate.Date

我也试过这个,但它正在回归&#39; 1&#39;每一天。

SELECT COUNT(*) cnt, MonthDate.Date
FROM  (
    SELECT 1 AS Date UNION ALL
    SELECT 2 UNION ALL
    SELECT 3 UNION ALL
    SELECT 4 UNION ALL
    SELECT 5 UNION ALL
    SELECT 6 UNION ALL
    SELECT 7 UNION ALL
    SELECT 8 UNION ALL
    SELECT 9 UNION ALL
    SELECT 10 UNION ALL
    SELECT 11 UNION ALL
    SELECT 12 UNION ALL
    SELECT 13 UNION ALL
    SELECT 14 UNION ALL
    SELECT 15 UNION ALL
    SELECT 16 UNION ALL
    SELECT 17 UNION ALL
    SELECT 18 UNION ALL
    SELECT 19 UNION ALL
    SELECT 20 UNION ALL
    SELECT 21 UNION ALL
    SELECT 22 UNION ALL
    SELECT 23 UNION ALL
    SELECT 24 UNION ALL
    SELECT 25 UNION ALL
    SELECT 26 UNION ALL
    SELECT 27 UNION ALL
    SELECT 28 UNION ALL
    SELECT 29 UNION ALL
    SELECT 30 UNION ALL
    SELECT 31) AS MonthDate
LEFT OUTER JOIN products IB 
ON ( MonthDate.date BETWEEN IB.arrived AND IB.released)
GROUP BY MonthDate.Date;

我希望看到输出如下,其中total是活动产品的数量,但是日期列不是必需的。

日期总计

2017-01-01 3024

2017-01-02 3029

...

2017-01-30 2987

还有其他建议吗?

1 个答案:

答案 0 :(得分:1)

有关生成日期范围的信息,请参阅this question;同样的方法应该适合你。我现在无法测试,但它看起来像是:

SET @date_min = '2017-01-01';
SET @date_max = '2017-01-31';

SELECT
   dg.date,
   COUNT(pb.id) as daily_count
from (
   select DATE_ADD(@date_min, INTERVAL (@i:=@i+1)-1 DAY) as `date`
   from information_schema.columns,(SELECT @i:=0) gen_sub 
   where DATE_ADD(@date_min,INTERVAL @i DAY) BETWEEN @date_min AND @date_max
) date_generator dg
left join products_booking pb on (pb.released IS NULL OR pb.released >= dg.date) AND (pb.arrived <= dg.date)
GROUP BY dg.date
ORDER BY dg.date
;