微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

php – 根据一个MySQL查询中的不同日期范围选择平均值语句

基本上我试图用这些数据制作图表.我可以将我的查询放入PHP中的while循环以获得每个平均值,但我更希望这是通过一个查询生成一个结果表来完成的.

<?PHP 

date_default_timezone_set('America/Los_Angeles');

include('../connect.PHP');

$subcategory = 'T-Shirts';

$date = date('Y-m-d', strtotime('-29 days'));
$today = date("Y-m-d");

$subcategory = MysqLi_real_escape_string($conp, $subcategory);

echo "<table border=\"1\">";
echo "<tr>";
echo "<th>date</th>";
echo "<th>average</th>";
echo "</tr>";

while (strtotime($date) <= strtotime($today)) {

    $from_date = date ("Y-m-d", strtotime("-29 day", strtotime($date)));

    $query = $conp->query("SELECT ROUND(SUM(OutCount)/30) AS 'average' FROM inventory
    LEFT JOIN item
    ON inventory.itemcode = item.itemcode
    WHERE item.subcategory = '$subcategory'
    AND TrDateTime BETWEEN '$from_date' AND '$date' AND transactiontype like 'OUT_%'"); 

    if($query->num_rows){       
        while($row = MysqLi_fetch_array($query, MysqL_ASSOC)){                      
            if(!empty($row['average'])){
                $average = $row['average'];
            }else{
                $average = "N/A";
            }
        }                       
        MysqLi_free_result($query);                             
    }else{
        $average = "N/A";
    }

    $date = date ("Y-m-d", strtotime("+1 day", strtotime($date)));

    echo "<tr>";
    echo "<td>" . $date . "</td>";
    echo "<td>" . $average . "</td>";
    echo "</tr>";
}

echo "</table>";

?>

获取过去30天(包括今天)的所有日期以及截至该日期前29天的平均销售额.

+------------+----------+  
| date       | average  |  
+------------+----------+  
| 2015-04-09 | 222      |  
| 2015-04-10 | 225      |  
| 2015-04-11 | 219      |  
| ...        | ...      |  
+------------+----------+  

我能够以这种方式获得我需要的所有东西,但是在这种情况下运行29次查询,MysqL会更快.我开始提出一个MysqL程序,但我不确定当我尝试用PHP调用它时它会有多好用.

DELIMITER //
    CREATE PROCEDURE average_daily_sales()
    BEGIN

        SET @today = CURDATE();
        SET @date_var = CURDATE() - INTERVAL 29 DAY;
        SET @from_date = @date_var - INTERVAL 29 DAY;
        SET @to_date = @from_date + INTERVAL 29 DAY;

        label1: WHILE @date_var < @today DO

            SELECT      DATE_FORMAT(trdatetime, '%Y-%m-%d') as 'date', ROUND(SUM(OutCount)/30) AS 'average'
            FROM        inventory
            LEFT JOIN   item
            ON          inventory.itemcode = item.itemcode
            WHERE       item.subcategory = 'T-Shirts'
            AND         trdatetime BETWEEN @from_date - INTERVAL 29 DAY AND @to_date
            AND         transactiontype like 'OUT_%';

            SET @date_var = @date_var + INTERVAL 1 DAY;

        END WHILE label1;    

    END; //
DELIMITER ;

最后,我更喜欢一个常规的MysqL语句,我可以用它来一次性生成所需的结果表.任何帮助将不胜感激.

解决方法:

您是否拥有该范围内每个不同日期的数据?如果是这样,这是一个稍微复杂的连接操作,但非常可行.

您可以按如下方式获取所需的日期范围:

        SELECT disTINCT
               DATE(trdatetime)- INTERVAL 30 DAY AS startdate,
               DATE(trdatetime)                  AS enddateplus1
          FROM inventory
         WHERE trdatetime >= Now() - INTERVAL 31 DAY

调试此查询.请查看以确保获得所需的每个日期范围.

然后,您可以将此加入到您的业务查询

  SELECT dates.startdate, 
         ROUND(SUM(OutCount)/30) AS 'average'
   FROM (
        SELECT disTINCT
               DATE(trdatetime)- INTERVAL 30 DAY AS startdate,
               DATE(trdatetime)                  AS enddateplus1
          FROM inventory
         WHERE trdatetime >= Now() - INTERVAL 31 DAY
        ) dates
   LEFT JOIN inventory  ON i.trdatetime >= dates.startdate
                       AND i.trdatetime <  dates.enddateplus1 
   LEFT JOIN  item ON  i.itemcode = item.itemcode
  WHERE item.subcategory = 'T-Shirts'
    AND transactiontype like 'OUT_%'
  GROUP BY dates.startdate

如果您的库存数据稀少,也就是说,您没有所有日期的交易,那么您的日期查询将缺少某些行.

有一种方法可以填补那些缺失的行.但这是一个痛苦的s.阅读本文以获取更多信息. http://www.plumislandmedia.net/mysql/filling-missing-data-sequences-cardinal-integers/

请注意,BETWEEN在过滤DATETIME或TIMESTAMP值方面确实非常糟糕.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐