PHP和MYSQL计算月份总数并将其复制到另一个表中


PHP and MYSQL calculate total of month and copy it to another table

我有表格(订单):id,价格,日期

id  |  price  |   date
01  |  250    |  2013-01-25
02  |  350    |  2013-01-25
03  |  25     |  2013-02-03
04  |  14     |  2013-03-21
05  |  96     |  2013-08-12
06  |  37     |  2013-08-12
07  |  89     |  2013-08-28
08  |  78     |  2013-12-20
09  |  45     |  2013-12-21

我想在每次执行scrp:时输出

  • 单日总价
  • 单月总价
  • 年度总价

我想在另一张表中插入所有天数的总和,如下图所示,其中包含全年天数(例如):

date        |  price
2013-01-01  |  5656
2013-01-02  |  659596
2013-01-03  |  5464

对于天总数,我使用了这个代码

...ecc..
$date = date("Y-m-d");
....ecc...
$query = "SELECT id, price FROM orders WHERE delivery_date LIKE '%$date%'";
$result = mysql_query($query) or die(mysql_error());
while ($row = mysql_fetch_array($result))
...ecc...

但我只能在当前日期做,不能在任何一天做。我一个月都做不到。。

您似乎在寻找SUMGROUP BY:

要获得每天的价格SUM,你可以做:

SELECT date, SUM(price) as price
FROM orders 
GROUP BY date
ORDER BY date;

sqlfiddle演示

这将为您提供日期和该日期的价格总额,只要该日期中有任何商品。

要将其插入另一个表,您可以执行以下操作:

INSERT INTO tab2
SELECT date, SUM(price) as price
FROM orders 
GROUP BY date