2015-08-23 83 views
0

我有這個查詢生成我想要的結果。 我只需要製造日期爲列日期作爲列(每天創建報告)

SELECT item, date, SUM(quantity) 
FROM t 
WHERE date between '2015-08-18' and '2015-08-20' 
GROUP BY item, date 

這裏是我的SQL FIDDLE

產生

Output

結果我想

result

任何人都可以給我至少想法如何實現我想要的結果?

+0

可能是最好做到這一點在PHP消耗原始查詢。只要您擴展日期範圍,查詢就必須更改,應用程序代碼可以保持不變。 – amdixon

回答

3

像這樣使用conditional Aggregate

要提取daydate使用DAY()功能

SELECT item, 
     SUM(case when day(date) = 18 then quantity else 0 end) as `18`, 
     SUM(case when day(date) = 19 then quantity else 0 end) as `19`, 
     SUM(case when day(date) = 20 then quantity else 0 end) as `20` 
FROM t 
WHERE date between '2015-08-18' and '2015-08-20' 
GROUP BY item 

SQL FIDDLE DEMO