SQL:月份查询

时间:2016-08-02 08:48:51

标签: php mysql

您好,我的数据库有一个简单的查询,根据我将使用下面的查询设置的自定义日期显示每笔交易的销售额。

示例我选择日期并根据我设定的日期显示我的报告。

现有查询

$strSQL =  "SELECT  IFNULL(`ProductName`, 'Total') AS 'ProductName',`
                    Closed On`, 
                    SUM(`Quantity`) as Quantity ,
                    SUM(`Price Paid`) as Price 
FROM `".$user."_products` 
WHERE DATE( `Closed On`) >= '$datefrom' 
AND DATE(`Closed On`) <='$dateto' 
GROUP by `ProductName` with ROLLUP";

现有输出

  ---------------------------------------
  |Product Name |Quantity |Total Sales  |
  ---------------------------------------
  |Shoes        | 12      |  1000       |
  |Tshirt       | 232     |  2312       |
  |Total        | 352     |  3312       |
  ---------------------------------------

但我想逐个月地查询我的表格示例输出是(jan-dec)

  --------------------------------------------
  |Product Name |Jan   |Feb   | March| Total  |
  --------------------------------------------
  |Shoes        | 500  | 1000 | 215  | 121542 |
  |Tshirt       | 232  | 2312 | 1235 | 123123 |
  --------------------------------------------

谢谢!

1 个答案:

答案 0 :(得分:0)

您可以使用条件总和来完成此操作。对于每个月,您检查该行是否为该月,如果是,请使用您想要求和的值,如果不是,则只使用0。

$strSQL =  "SELECT  IFNULL(`ProductName`, 'Total') AS 'ProductName',`
                    Closed On`, 
                    SUM(IF(MONTH(`Closed On`) = 1, `Quantity`, 0)) AS `Jan`,
                    SUM(IF(MONTH(`Closed On`) = 2, `Quantity`, 0)) AS `Feb`,
                    SUM(IF(MONTH(`Closed On`) = 3, `Quantity`, 0)) AS `Mar`,
                    SUM(IF(MONTH(`Closed On`) = 4, `Quantity`, 0)) AS `Apr`,
                    SUM(IF(MONTH(`Closed On`) = 5, `Quantity`, 0)) AS `May`,
                    SUM(IF(MONTH(`Closed On`) = 6, `Quantity`, 0)) AS `Jun`,
                    SUM(IF(MONTH(`Closed On`) = 7, `Quantity`, 0)) AS `Jul`,
                    SUM(IF(MONTH(`Closed On`) = 8, `Quantity`, 0)) AS `Aug`,
                    SUM(IF(MONTH(`Closed On`) = 9, `Quantity`, 0)) AS `Sep`,
                    SUM(IF(MONTH(`Closed On`) = 10, `Quantity`, 0)) AS `Oct`,
                    SUM(IF(MONTH(`Closed On`) = 11, `Quantity`, 0)) AS `Nov`,
                    SUM(IF(MONTH(`Closed On`) = 12, `Quantity`, 0)) AS `Dec`,
                    SUM(`Quantity`) as Quantity ,
                    SUM(`Price Paid`) as Price 
FROM `".$user."_products` 
WHERE DATE( `Closed On`) >= '$datefrom' 
AND DATE(`Closed On`) <='$dateto' 
GROUP by `ProductName` with ROLLUP";

修改

年复一年会更棘手。作为1关,它几乎与上面相同,只是检查年份而不是月份。但是,如果这是用于在线报告,那么您可能希望多年的窗口移动(即,您可能对2011年至2016年感兴趣,但在2020年可能已经移至2015年至2020年)。在这种情况下,您可能需要在php(甚至是MySQL存储过程)中动态构建查询。有很多方法可以解决这个问题,但它们仍然会很混乱(你无法在查询中真正更改返回的列名,而是需要提前更改查询以提供所需的返回列名。)

基本查询: -

$strSQL =  "SELECT  IFNULL(`ProductName`, 'Total') AS 'ProductName',
                    `Closed On`, 
                    SUM(IF(YEAR(`Price Paid`) = 2006, `Quantity`, 0)) AS `2006`,
                    SUM(IF(YEAR(`Price Paid`) = 2007, `Quantity`, 0)) AS `2007`,
                    SUM(IF(YEAR(`Price Paid`) = 2008, `Quantity`, 0)) AS `2008`,
                    SUM(IF(YEAR(`Price Paid`) = 2009, `Quantity`, 0)) AS `2009`,
                    SUM(IF(YEAR(`Price Paid`) = 2010, `Quantity`, 0)) AS `2010`,
                    SUM(IF(YEAR(`Price Paid`) = 2011, `Quantity`, 0)) AS `2011`,
                    SUM(IF(YEAR(`Price Paid`) = 2012, `Quantity`, 0)) AS `2012`,
                    SUM(IF(YEAR(`Price Paid`) = 2013, `Quantity`, 0)) AS `2013`,
                    SUM(IF(YEAR(`Price Paid`) = 2014, `Quantity`, 0)) AS `2014`,
                    SUM(IF(YEAR(`Price Paid`) = 2015, `Quantity`, 0)) AS `2015`,
                    SUM(IF(YEAR(`Price Paid`) = 2016, `Quantity`, 0)) AS `2016`,
                    SUM(IF(YEAR(`Price Paid`) = 2017, `Quantity`, 0)) AS `2017`,
                    SUM(`Quantity`) as Quantity ,
                    SUM(`Price Paid`) as Price 
FROM `".$user."_products` 
WHERE DATE( `Closed On`) >= '$datefrom' 
AND DATE(`Closed On`) <='$dateto' 
GROUP by `ProductName` with ROLLUP";