MYSQL包括零项目计数的日期值

时间:2012-06-28 02:18:40

标签: mysql sql datetime count

我想计算一次购买的总次数以及按item_id购买的次数。在该示例中,用户可以拥有项目,并且这些项目可以由其他用户购买。所有者无法购买自己的物品。

我遇到的问题是如何在没有购买数天的情况下返回计数为“0”的结果以及正整数计数的日期。

这是我的表格:

      items           |          items_purchased          | numbers |   dates
i_id  item_id user_id | p_id item_id  user_id     date    |   num   | datefield
  1      1       11   |  1      1         13   2009-01-11 | 1       | 2005-06-07
  2      2       12   |  2      1         14   2009-01-11 | 2       | 2005-06-08
  3      3       11   |  3      2         15   2009-01-12 | 3       | 2005-06-09   
                      |  4      3         16   2009-01-12 | ...     | ...
                      |  5      1         17   2011-12-12 | 1000    | 2015-06-07

以下是我对user_id=11项目购买总数的MYSQL查询:

SELECT COUNT(*) as counts, DATE(purchase_date) as DATE
FROM items_purchased 
JOIN items on items_purchased.item_id=items.item_id 
WHERE items.user_id=11 
GROUP BY DATE(purchase_date)
//note this query **doesn't** make use of the numbers and dates tables b/c I don't know how to use them

结果如下:

counts    date
  2    2009-01-11
  1    2009-01-12
  1    2011-12-12

以下是我想要看到的内容:

counts    date
  2    2009-01-11
  1    2009-01-12
  0    2009-01-13
  0    ... // should be a row here for each day between 2009-01-13 and 2011-12-12
  1    2011-12-12
  0    ... // should be a row here for each day between 2011-12-12 and current date
  0    current date (2012-6-27)

以下是我的MYSQL查询,其中包含item_id=1所拥有的user_id=11限制购买总数:

SELECT COUNT(*) as counts, DATE(purchase_date) as DATE
FROM items_purchased 
JOIN items on items_purchased.item_id=items.item_id 
WHERE items.user_id=11 and items.item_id=1
GROUP BY DATE(purchase_date)

结果如下:

counts    date
  2    2009-01-11
  1    2011-12-12

与上述类似,以下是我想要看到的内容:

counts    date
  2    2009-01-11
  0    2009-01-12
  0    ... // should be a row here for each day between 2009-01-12 and 2011-12-12
  1    2011-12-12
  0    ... // should be a row here for each day between 2011-12-12 and current date
  0    current date (2012-6-27)

不知何故,我认为我需要合并numbersdates表,但我不知道该怎么做。任何想法都将不胜感激,

感谢, 添

1 个答案:

答案 0 :(得分:4)

为修正答案而编辑:

http://sqlfiddle.com/#!2/ae665/4

SELECT date_format(datefield,'%Y-%m-%d') AS DATE, IFNULL(counts, 0), item_id FROM 
    dates a
LEFT JOIN 
    (SELECT COUNT(*) as counts, purchase_date,user_id,item_id 
     FROM items_purchased 
     WHERE item_id=1
     GROUP BY date(purchase_date),item_id )r 
ON date(a.datefield) = date(r.purchase_date) ;

以上查询基于以下假设:

  
      
  1. 表格日期包含您要列出的日期范围内的日期顺序。
  2.   
  3. 不确定什么是项目表。第二个查询是按照purchase_date和items_prechased table的item_id进行分组。
  4.   
  5. 计数是计算在特定日期购买的特定商品(无论user_id如何)。
  6.   

@timpeterson(OP)更新 非常感谢@Sel。这里的sqlfiddles展示了我感兴趣的两个查询:

  1. 单个用户拥有的所有商品的购买/天(例如user_id=11):http://sqlfiddle.com/#!2/76c00/3
  2. item_id=1拥有的user_id=11的购买/日期: http://sqlfiddle.com/#!2/76c00/1
  3. 这是第二个的SQL代码,以防链接以某种方式被破坏:

    SELECT date_format(datefield,'%Y-%m-%d') AS DATE, 
    IFNULL(countItem, 0), item_id
    FROM dates a
    LEFT JOIN 
    (SELECT countItem, purchase_date,i.user_id,p.item_id FROM (
       SELECT count(*) as countItem, purchase_date,user_id,item_id 
       FROM items_purchased 
       GROUP BY date(purchase_date),item_id
       ) p 
     inner join items i
     on i.item_id=p.item_id
     WHERE p.item_id='1' and i.user_id='11' //just get rid of "p.item_id='1'" to produce the 1st query result
    )r 
    ON date(a.datefield) = date(r.purchase_date);