Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/275.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/three.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 基于用户输入的动态mysql查询_Php_Mysql - Fatal编程技术网

Php 基于用户输入的动态mysql查询

Php 基于用户输入的动态mysql查询,php,mysql,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 =

我有一个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月)


谢谢

您的方法仅在一年内有效;否则,如果您不想将多个年份中的月份分组在一起,则需要额外的列。您还应该使用
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 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
从SellerName中选择SUM(ReportMonth)
BuyerZipCode在哪里($zips\u查询)
和ReportMonth>“$start\u date”
和ReportMonth
e、 g

从SellerName中选择SUM(ReportMonth)
其中BuyerZipCode在(1,2,3,4,5,6)中
和报告月份>'2013-01-01'
报告月份<日期添加('2013-01-01',间隔1年)
按月分组

我对多年来在一起的几个月都很满意——如果最初的问题是关于这一点的粗略描述,我深表歉意。假设我可以将ReportMonth字段更改为date字段-我仍然需要对给定月份的所有销售额求和-并根据用户选择的月份进行调整。用户可以选择1个月、3个月、24个月或任何介于这两者之间的工作时间,非常感谢。我做了一个小改动-我需要一个“,”1,0“在这里:”和右(ReportMonth,2)=01,1,0))…还有-因为计数(*),我不需要if/then语句。我需要一个“,”在所有“总和”的末尾“sI应该在左边添加一个表,表中有卖家,月份是标题。用户将根据开始月份和结束月份指定表中的列数。因此,当用户跨入新的一年时,如果标题为:10月| 11月| 12月| 1月| 2月等,我就可以了。
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