【问题标题】:Loop through dates to find specific day php SQL遍历日期以查找特定日期 php SQL
【发布时间】:2014-11-22 15:41:03
【问题描述】:

PHP MYSQL - 如何仅计算每天的价格总和?如何判断新的一天何时从 0 重新开始计数。有没有办法说,现在是第 26 天,让我们从 0 重新开始计数,以便单独保存信息。在我的 SQL 表中,我有两列 Price 和 Lastvisit,25 日的最终结果应该是 289,而 26 日的总结果应该是 44+90+10 所以 144。如果可以,请提供帮助。谢谢。

  • 价格
  • 20
  • 50
  • 65
  • 55
  • 50
  • 15
  • 34
  • 44
  • 90
  • 10

  • 上次访问

  • 2014-09-25
  • 2014-09-25
  • 2014-09-25
  • 2014-09-25
  • 2014-09-25
  • 2014-09-25
  • 2014-09-25
  • 2014-09-26
  • 2014-09-26
  • 2014-09-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;
    ?>
    

【问题讨论】:

  • 为什么我们要处理表格 RBAR 中的行(逐行处理)?
  • 呃,我知道。这段代码是我沮丧的产物。感谢 John,新的完整代码总共 10 行。感谢您的光临。

标签: php mysql date loops sum


【解决方案1】:

是我遗漏了什么还是你只想要总和?

SELECT SUM(Price) AS Total
FROM History
GROUP BY Lastvisit;

【讨论】:

  • 不,你没有遗漏任何东西。由于缺乏 SQL 知识,我使 PHP 过于复杂。谢谢约翰,我现在可以停止用脸撞桌子了。先生,您好。
  • 哦,好吧 :) 我在想我一定错过了一些东西,哈哈,很高兴我能帮上忙!如果您可以接受答案,将不胜感激! +1 回报:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-04-03
  • 1970-01-01
相关资源
最近更新 更多