0

我有一个mysql查询,目前按月汇总数据:

    $sql = SELECT SellerName, 
           SUM(IF(ReportMonth = 201201, 1,0)) AS Jan, 
           SUM(IF(ReportMonth = 201202, 1,0)) AS Feb, 
           SUM(IF(ReportMonth = 201203, 1,0)) AS Mar, 
           SUM(IF(ReportMonth = 201204, 1,0)) AS Apr, 
           SUM(IF(ReportMonth = 201205, 1,0)) AS May, 
           SUM(IF(ReportMonth = 201206, 1,0)) AS Jun,
           SUM(IF(ReportMonth = 201207, 1,0)) AS Jul,
           SUM(IF(ReportMonth = 201208, 1,0)) AS Aug,     
           SUM(IF(ReportMonth = 201209, 1,0)) AS Sep,
           SUM(IF(ReportMonth = 201210, 1,0)) AS Oct,      
           SUM(IF(ReportMonth = 201211, 1,0)) AS Nov,    
           COUNT(*) AS YTD
           FROM onlineDATA
           WHERE BuyerZipCode IN ($zips_query) 
           GROUP BY SellerName  

这很好用。

但是,我需要调整它以允许用户输入月份 - 即用户将选择开始和结束月份。

我可以将 datepicker 数据格式化为现有的 yyyymm 格式 - 但是我将如何在 PHP 中制定查询以调整可变月份并考虑多年(例如 2012 年 10 月至 2013 年 2 月)?

谢谢!

4

2 回答 2

0

你的方法只适用于一年;否则,如果您不希望将多年中的月份组合在一起,则需要额外的列。您还应该使用date函数而不是检查整数值(您不应该将日期存储为 int)。

如果您需要动态方法,则需要在 PHP 端进行动态查询准备(获取开始/结束日期并生成 SQL)。

这可能是您正在寻找的解决方案:

<?php

$startDate = '201201';
$endDate = '201303';

$formattedStartDate = strtotime( SUBSTR($startDate, 0, 4) . '-' . SUBSTR($startDate, -2, 2) . '-01' );
$formattedEndDate = strtotime( SUBSTR($endDate, 0, 4) . '-' . SUBSTR($endDate, -2, 2) . '-01' );

$sql = "SELECT SellerName, \n";

while ($formattedStartDate <= $formattedEndDate) {
    $sql .= 'SUM(IF(LEFT(ReportMonth, 4) = ' . date('Y', $formattedStartDate) . ' AND RIGHT(ReportMonth, 2) = ' . date('m', $formattedStartDate) . ")) AS '" . date('M Y', $formattedStartDate);

    if($formattedStartDate <> $formattedEndDate) {
        $sql .= "', \n";
    }
    else {
        $sql .= "'\n";
    }

    $formattedStartDate = strtotime('+1 month', $formattedStartDate);
}

$sql .= 'COUNT(*) AS YTD
FROM onlineDATA
WHERE BuyerZipCode IN ($zips_query) 
GROUP BY SellerName';

echo $sql;

结果

选择卖家姓名,
SUM(IF(LEFT(ReportMonth, 4) = 2012 AND RIGHT(ReportMonth, 2) = 01)) AS 'Jan 2012',
SUM(IF(LEFT(ReportMonth, 4) = 2012 AND RIGHT(ReportMonth, 2) = 02)) AS 'Feb 2012',
SUM(IF(LEFT(ReportMonth, 4) = 2012 AND RIGHT(ReportMonth, 2) = 03)) AS 'Mar 2012',
SUM(IF(LEFT(ReportMonth, 4) = 2012 AND RIGHT(ReportMonth, 2) = 04)) AS 'Apr 2012',
SUM(IF(LEFT(ReportMonth, 4) = 2012 AND RIGHT(ReportMonth, 2) = 05)) AS 'May 2012',
SUM(IF(LEFT(ReportMonth, 4) = 2012 AND RIGHT(ReportMonth, 2) = 06)) AS 'Jun 2012',
SUM(IF(LEFT(ReportMonth, 4) = 2012 AND RIGHT(ReportMonth, 2) = 07)) AS 'Jul 2012',
SUM(IF(LEFT(ReportMonth, 4) = 2012 AND RIGHT(ReportMonth, 2) = 08)) AS 'Aug 2012',
SUM(IF(LEFT(ReportMonth, 4) = 2012 AND RIGHT(ReportMonth, 2) = 09)) AS 'Sep 2012',
SUM(IF(LEFT(ReportMonth, 4) = 2012 AND RIGHT(ReportMonth, 2) = 10)) AS 'Oct 2012',
SUM(IF(LEFT(ReportMonth, 4) = 2012 AND RIGHT(ReportMonth, 2) = 11)) AS 'Nov 2012',
SUM(IF(LEFT(ReportMonth, 4) = 2012 AND RIGHT(ReportMonth, 2) = 12)) AS 'Dec 2012',
SUM(IF(LEFT(ReportMonth, 4) = 2013 AND RIGHT(ReportMonth, 2) = 01)) AS 'Jan 2013',
SUM(IF(LEFT(ReportMonth, 4) = 2013 AND RIGHT(ReportMonth, 2) = 02)) AS 'Feb 2013',
SUM(IF(LEFT(ReportMonth, 4) = 2013 AND RIGHT(ReportMonth, 2) = 03)) AS 'Mar 2013'
计数(*) 为年初至今
来自在线数据
WHERE BuyerZipCode IN ($zips_query)
按卖家名称分组

查看演示

于 2013-03-11T15:31:14.797 回答
0

你应该能够做到这一点,我敢肯定这是一个开始:

SELECT SUM(ReportMonth) FROM SellerName
WHERE BuyerZipCode IN ($zips_query)
AND ReportMonth > '$start_date'
AND ReportMonth < DATE_ADD('$start_date', INTERVAL 1 YEAR)
GROUP BY ReportMonth

例如

SELECT SUM(ReportMonth) FROM SellerName
WHERE BuyerZipCode IN (1,2,3,4,5,6)
AND ReportMonth > '2013-01-01'
AND ReportMonth < DATE_ADD('2013-01-01', INTERVAL 1 YEAR)
GROUP BY ReportMonth
于 2013-03-11T15:40:36.573 回答