0

从 MySQL 表中提取数据,数据如下所示:

Acct #          Acct Name                  Item     Date Sold            Qty Sold 
10293           Company A               Canon JX200 2012-06-24                1
11042           Company B               Canon JX200 2012-06-05                2
12560           Company D               Canon L90   2012-04-20                1
12560           Company D               Canon L90   2012-05-07                1
14160           Company D               Canon L90   2012-05-22                2
14168           Company C               Canon L90   2012-04-20                6

我想要如下所示的输出:

Acct #  Acct Name   Item          Mo 1  Mo 2    Mo 3    Mo 4    Mo 5    Mo 6    Tot
10293   Company A   Canon JX200                                           1       1
11042   Company B   Canon JX200                                           2       2
12560   Company C   Canon L90                             6                       6
12560   Company D   Canon L90                             1       3               4

大学里的 Aced 2 线性代数课程和 php 数组让我难过。需要将我的数据放入数组,排序和汇总。最大的问题(我认为)是构建数组的代码。提前致谢!

4

2 回答 2

0

尝试通过 addinf ORDER BY Acct Name对 MySQL 查询中的数据进行排序。当您收到数据时,此代码应该会有所帮助:

$data = array();
foreach ($rows as $row) {
preg_match('/^\d{4}\-(\d{2})\-/', $row['DateSold'], $match);
  $data[] = array(
   'Acct' => $row['Acct'],
   'AcctName' => $row['AcctName'],
   'Item' => $row['Item'],
   'Mo ' . $match[1] => $row['QtySold']
  );

}
于 2012-07-26T07:45:35.937 回答
0

为此,您可以使用数据透视表。http://en.wikibooks.org/wiki/MySQL/Pivot_table

于 2012-07-26T07:13:02.130 回答