我想计算总购买次数以及按 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)
不知何故,我认为我需要合并numbers
和dates
表,但我不知道该怎么做。任何想法将不胜感激,
谢谢,蒂姆