php mysql今天的总和和月总和

时间:2015-02-17 03:30:18

标签: php mysql date sum

我正在尝试计算今天的销售总额,但这不起作用。 它有效,如果我删除日期部分...

# This works and will gives out the total:
$result = mysql_query("SELECT SUM(grand_total) AS value_sum FROM order");
$row = mysql_fetch_assoc($result);
$sum = $row['value_sum'];

# This doesn't work:
$result = mysql_query("SELECT SUM(grand_total) AS value_sum FROM new_order WHERE date = CURDATE()");
$query = mysql_query($result) or die ('Error: ' . mysql_error());
$row = mysql_fetch_assoc($result);
$sum = $row['value_sum'];

此外,如何在此之后获得此专栏的每月总数?

1 个答案:

答案 0 :(得分:0)

如果date字段为DATETIME类型,则尝试此sql语句:

SELECT SUM(`grand_total`) AS value_sum 
FROM `order`
WHERE DATE(`date`) = CURDATE()