sql / php - 水平和垂直显示记录

时间:2013-01-17 00:59:41

标签: php mysql sql pivot unpivot

所以我从基本的mySQL选择查询获得这些数据,每行输出。

-Basic mySQL select query

ID---------DATE---------POINT1---POINT2------TOTAL
1----2013-01-03----------10----------16---------26
2----2013-01-03----------11----------22---------33
3----2013-01-03----------15----------7----------22
1----2013-01-04----------20----------4----------24
2----2013-01-04----------8-----------32---------40
3----2013-01-04----------16----------12---------28
1----2013-01-05----------12----------17---------29
2----2013-01-05----------2-----------29---------31
3----2013-01-05----------8-----------10---------18

我想要做的是按日期对列中的数据进行排序,并按行对id进行动态排序,例如每月。这是所需的输出,

/----------/2013-01-03/---------/2013-01-04/------/2013-01-05/------/
ID--Point 1-Point2-Total-Point 1-Point2-Total-Point 1-Point2-Total
1----10-------16-----26-----20-------4-----24----12-----17------29
2----11-------22-----33------8------32-----40-----2-----29------31
3----15-------7------22-----16------12-----28-----8-----10------18

然后将其输出到csv或excel。我有点失去了如何实现这一目标。如果有人可以指导我,那就太好了。感谢

1 个答案:

答案 0 :(得分:0)

您可以取消列中的数据,然后执行 pivot 将所有数据转换回列:

select id,
  sum(case when date='2013-01-03' and col='Point1' then value end) Point1_01032013,
  sum(case when date='2013-01-03' and col='Point2' then value end) Point2_01032013,
  sum(case when date='2013-01-03' and col='Total' then value end) Total_01032013,
  sum(case when date='2013-01-04' and col='Point1' then value end) Point1_01042013,
  sum(case when date='2013-01-04' and col='Point2' then value end) Point2_01042013,
  sum(case when date='2013-01-04' and col='Total' then value end) Total_01042013,
  sum(case when date='2013-01-05' and col='Point1' then value end) Point1_01052013,
  sum(case when date='2013-01-05' and col='Point2' then value end) Point2_01052013,
  sum(case when date='2013-01-05' and col='Total' then value end) Total_01052013
from
(
  select id, date_format(date, '%Y-%m-%d') date, 'Point1' col, Point1 as value
  from yourtable
  union all
  select id, date_format(date, '%Y-%m-%d') date, 'Point2' col, Point2
  from yourtable
  union all
  select id, date_format(date, '%Y-%m-%d') date, 'Total' col, Total
  from yourtable
) src
group by id;

请参阅SQL Fiddle with Demo

结果是:

| ID | POINT1_01032013 | POINT2_01032013 | TOTAL_01032013 | POINT1_01042013 | POINT2_01042013 | TOTAL_01042013 | POINT1_01052013 | POINT2_01052013 | TOTAL_01052013 |
---------------------------------------------------------------------------------------------------------------------------------------------------------------------
|  1 |              10 |              16 |             26 |              20 |               4 |             24 |              12 |              17 |             29 |
|  2 |              11 |              22 |             33 |               8 |              32 |             40 |               2 |              29 |             31 |
|  3 |              15 |               7 |             22 |              16 |              12 |             28 |               8 |              10 |             18 |