动态mysql基于用户输入查询

时间:2013-03-11 15:26:34

标签: php mysql

我有一个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年8月至2013年2月)?

谢谢!

2 个答案:

答案 0 :(得分: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;

<强>结果

SELECT SellerName, 
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'
COUNT(*) AS YTD
FROM onlineDATA
WHERE BuyerZipCode IN ($zips_query) 
GROUP BY SellerName

See the demo

答案 1 :(得分: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

e.g。

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