3

我想计算总购买次数以及按 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

这是我的 MYSQL 查询的商品购买总数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 
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)

item_id=1这是我的 MYSQL 查询,查询仅限于拥有的购买总数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表,但我不知道该怎么做。任何想法将不胜感激,

谢谢,蒂姆

4

1 回答 1

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. 不太确定项目表的用途。第二个查询是按 items_purchased 表的 purchase_date 和 item_id 分组。
  3. 计数是计算在特定日期购买的特定项目(与 user_id 无关)。

@timpeterson (OP) 更新主要感谢@Sel。这是演示我感兴趣的两个查询的 sqlfiddles:

  1. 单个用户拥有的所有项目的购买次数/天(例如,user_id=11):http ://sqlfiddle.com/#!2/76c00/3
  2. 购买/天item_id=1拥有者user_id=11http ://sqlfiddle.com/#!2/76c00/1

这是第二个的 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);
于 2012-06-28T03:20:26.177 回答