选择日期作为摘要表的列

时间:2014-04-28 14:47:17

标签: mysql date

从这个示例表和数据:

CREATE TABLE sales
  (`sales_person_id` int, 
   `sale_date` datetime, 
   `sale_amount` int);

INSERT INTO sales
  (`sales_person_id`, `sale_date`, `sale_amount`)
VALUES
  (1, '2014-01-01 00:00:00', 10),
  (1, '2014-01-02 00:00:00', 12),
  (2, '2014-01-03 00:00:00', 20),
  (1, '2014-01-03 00:00:00', 15),
  (1, '2014-01-03 00:00:00', 3),
  (3, '2014-01-05 00:00:00', 25)
;

Fiddle for convenience

使用@startDate = '2014/01/01'@endDate = '2014/01/06'时,我想以这种格式返回结果:

ID | 20140101 | 20140102 | 20140103 | 20140104 | 20140105 | 20140106 | 
----------------------------------------------------------------------
1  |       10 |       12 |       18 |        0 |        0 |        0 |
2  |        0 |        0 |       20 |        0 |        0 |        0 |
3  |        0 |        0 |        0 |        0 |       25 |        0 |
----------------------------------------------------------------------

我确信这可以在一个查询中完成,但我目前还没有任何结果。如果我取得任何进展,我会进行编辑。

0 个答案:

没有答案