循环查看日期以查找特定日期的PHP SQL

问题描述:

PHP MYSQL - How do I calculate the sum of prices for each day only? How can I tell when a new day starts to begin counting again from 0. Is there a way to say, The day is the 26th now, lets start counting again from 0 in order to save the information separately. In my SQL table I have two columns Price and Lastvisit, the final outcome for the 25th should be 289, and 26th should total 44+90+10 so 144. Please help if you can. Thanks.

  • Price
  • 20
  • 50
  • 65
  • 55
  • 50
  • 15
  • 34
  • 44
  • 90
  • 10

  • Lastvisit

  • 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;
    ?>
    

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

  • 价格 li>
  • 20 li>
  • 50 li>
  • 65
  • 55 LI>
  • 50 LI>
  • 15 LI>
  • 34 LI>
  • 44
  • 90 LI>
  • 10 p> LI>

  • Lastvisit p> LI> 2014年9月25日 LI>

  • 2014年9月25日 LI>
  • 2014年9月25日 LI>
  • 2014年9月25日
  • 2014年9月25日 LI>
  • 2014年9月25日 LI>
  • 2014年9月25日 LI>
  • 2014年9月26日 LI>
  • 2014年9月26日 LI>
  • 2014年9月26日 p>

       &lt;?php 
     $ date_start = date(“Y / m / d”);  //今天日期
     $ end_date =日期(“Y / m / d”,strtotime(“上个月”,strtotime($ date_start)));  //前30天
     
     
    时间(strtotime($ date_start)&gt; = strtotime($ end_date)){//贯穿30天中的每一天
     $ date_start = date(“Y / m / d”,strtotime)  (“-1天”,strtotime($ date_start)));  //一次删除1天。
     // echo $ date_start。  “&lt; br&gt;”; 
     $ lastthirtydays。= substr($ date_start,8)。“|”;  //每天爆炸以插入
     $ linegraphq =“SELECT History.Price,History.Lastvisit FROM History WHERE Lastvisit ='$ date_start'”;  // sql 
     $ linegraphqresult = mysql_query($ linegraphq);  //// query 
    while($ row = mysql_fetch_array($ linegraphqresult)){//获取查询的所有结果
     $ dayofprice。= substr($ row ['Lastvisit'],8);  //仅爆炸每一天#
     
     // @ echo“COST:”。  $ row ['Price']。  “日期”。  $ row ['Lastvisit']。  “&LT峰; br&gt;” 中;  //这只是我正在使用的信息。
     $ finalcost + = $ row ['Price'];  //这是汇总所有30天的定价
     $ pricetoday。= $ row ['Price']。  “|”  。  $行[ 'Lastvisit']。  “|”;  //现在你可以告诉我迷路了...... 
     
    } 
    } 
    echo $ dayofprice;  // TEST 
    echo $ pricetoday;  // TEST 
     $ explosionmonth = explode('|',$ lastthirtydays); 
     $ explosionprice = explode('|',$ pricetoday); 
     // echo $ explosionprice [1]; 
     // echo $  finalcost; 
    ?&gt; 
      code>  pre>  li> 
      ul> 
      div>

Am I missing something or do you just want the sum?

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