PHP MYSQL - 如何仅计算每天的价格总和?如何判断新的一天何时从0开始重新开始计数。有没有办法说,现在是第26天,让我们从0再次开始计数以便分别保存信息。在我的SQL表中,我有两列Price和Lastvisit,25日的最终结果应该是289,26th应该总计44 + 90 + 10所以144.如果可以,请帮助。感谢。
10
Lastvisit
2014年9月26日
<?php
$date_start = date("Y/m/d"); //Todays Date
$end_date = date ("Y/m/d", strtotime("last month", strtotime($date_start))); //30 days prior
while (strtotime($date_start) >= strtotime($end_date)) { //run through each of the 30 days
$date_start = date ("Y/m/d", strtotime("-1 day", strtotime($date_start))); //remove 1 day at a time.
//echo $date_start . "<br>";
$lastthirtydays .= substr($date_start,8)."|"; //explode each day to plug it in later
$linegraphq = "SELECT History.Price,History.Lastvisit FROM History WHERE Lastvisit='$date_start'"; //sql
$linegraphqresult = mysql_query($linegraphq); ////query
while($row = mysql_fetch_array($linegraphqresult)){ //get all results from query
$dayofprice .= substr($row['Lastvisit'],8); //explode each DAY # only.
//echo "COST: " . $row['Price'] . "DATE" . $row['Lastvisit'] . "<br>"; //this is just the info im working with.
$finalcost += $row['Price']; //this is summing all of the 30 days pricing
$pricetoday .= $row['Price'] . "|" . $row['Lastvisit']. "|"; //now you can tell im getting lost...
}
}
echo $dayofprice; //TEST
echo $pricetoday; //TEST
$explodedmonth = explode('|',$lastthirtydays);
$explodedprice = explode('|',$pricetoday);
//echo $explodedprice[1];
//echo $finalcost;
?>
答案 0 :(得分:1)
我错过了什么,或者你只想要总和?
SELECT SUM(Price) AS Total
FROM History
GROUP BY Lastvisit;