2012-09-30 77 views
3

我使用下面顯示添加在過去7天的產品個性化......我可以採用某種定製查詢,以顯示所有過去的7天,即使COUNT = 0?顯示值即使空

查詢,因爲它代表:

SELECT DAYNAME(dateadded) DAY, COUNT(*) COUNT 
FROM `products` 
WHERE (`dateadded` BETWEEN DATE_SUB(CURDATE(), INTERVAL 7 DAY) AND CURDATE() && site_url = 'mysite.com') 
GROUP BY DAY(dateadded) 

回答

1

添加一個表,在它時間(一個日期查找表),則:

SELECT DAYNAME(d.FullDate) DAY, COUNT(*) COUNT 
FROM dates d 
LEFT OUTER JOIN products p ON d.FullDate = DATE(p.dateadded) 
    AND p.site_url = 'mysite.com' 
WHERE d.FullDate BETWEEN DATE_SUB(CURDATE(), INTERVAL 7 DAY) AND CURDATE()  
GROUP BY d.FullDate 

它需要儲存的一點點,是的,但它會使這樣的查詢變得更容易。

或者,你可以通過在7天前和今天的日期循環,並返回一個排的每個存儲過程。