简体   繁体   中英

dynamic mysql Query based on user input

I have a mysql query that currently sums data by month:

    $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  

This works great.

But, I need to adjust it to allow for user input on the months - ie user will select start and end months.

I can format the datepicker data into the existing yyyymm format - but how would I formulate the query in PHP to adjust for variable months and accounting for multiple years (eg oct 2012 to Feb 2013)?

Thanks!

Your approach will only work for a single year; otherwise you'll need additional columns if you do not want months from multiple years grouped together. You should also use date functions rather than checking an integer value (you shouldn't be storing date as an int).

If you need a dynamic approach, you will need to do the dynamic query preparation on the PHP side (take the start/end date and generate the SQL).

Here may be a solution you're looking for:

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

Result

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

You should be able to do this, I'm sure something like this is a start:

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

eg

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM