需要显示复杂子查询的最近6个月的费用

时间:2017-06-26 12:23:34

标签: mysql select

我知道如何在单个查询中获取过去6个月的数据,但我有一个查询可以从多个子查询中获取数据,我需要显示最近6个月。

这是我当前的查询

SELECT `totalSales`,`totalPurchase`,`totalNetpay`,`totalPension`,`totalP32`
FROM
(SELECT SUM((`Unit_Cost`*`ExchangeRate`)*`Quantity`) AS `totalSales` FROM `salesinvoice_products` AS `prod`
LEFT JOIN `salesinvoice` AS `inv` on `prod`.`SalesInvoice_id`=`inv`.`SalesInvoice_id`) as `t1`,

(SELECT SUM((`InvoiceValue`*`ExchangeRate`)-`VATValue`) as `totalPurchase` FROM `purchaseinvoice`) as `t2`,

(SELECT SUM(`NetAmountPaid`) as `totalNetpay` FROM `payroll_netpay`) as `t3`,

(SELECT SUM(`totalAmountDue`) as `totalPension` FROM `payroll_pensionrecords`) as `t4`,

(SELECT SUM(`totalAmountDue`) as `totalP32` FROM `payroll_p32records`) as `t5`

如你所见,我有5个子查询。目前这给了我输出 3122.051719,2173.911200,10019.00,7351.56,2231.43

但我想要的是从最近6个月获得6行。然而,当我尝试这样做时,我得到多行代替6.我想我不得不以某种方式对它们进行分组。

好的,现在我将向您展示我试图让它工作的代码。

SELECT `InvoiceDate`,`totalSales`,`totalPurchase`,`totalNetpay`,`totalPension`,`totalP32`
FROM
(SELECT SUM((`Unit_Cost`*`ExchangeRate`)*`Quantity`) AS `totalSales`,`InvoiceDate` FROM `salesinvoice_products` AS `prod`
LEFT JOIN `salesinvoice` AS `inv` on `prod`.`SalesInvoice_id`=`inv`.`SalesInvoice_id`
WHERE (`InvoiceDate` < Now() and `InvoiceDate` > DATE_ADD(Now(), INTERVAL- 6 MONTH)) && `InvoiceType`>1
 GROUP BY MONTH(`InvoiceDate`)
) as `t1`,

(SELECT SUM((`InvoiceValue`*`ExchangeRate`)-`VATValue`) as `totalPurchase` FROM `purchaseinvoice`
WHERE (`InvoiceDate` < Now() and `InvoiceDate` > DATE_ADD(Now(), INTERVAL- 6 MONTH))
 GROUP BY MONTH(`InvoiceDate`)
) as `t2`,


(SELECT SUM(`NetAmountPaid`) as `totalNetpay` FROM `payroll_netpay`
 WHERE (`Date` < Now() and `Date` > DATE_ADD(Now(), INTERVAL- 6 MONTH))
  GROUP BY MONTH(`Date`)
) as `t3`,


(SELECT SUM(`totalAmountDue`) as `totalPension` FROM `payroll_pensionrecords`
 WHERE (`datePaid` < Now() and `datePaid` > DATE_ADD(Now(), INTERVAL- 6 MONTH))
  GROUP BY MONTH(`datePaid`)
 ) as `t4`,

(SELECT SUM(`totalAmountDue`) as `totalP32` FROM `payroll_p32records`
  WHERE (`datePaid` < Now() and `datePaid` > DATE_ADD(Now(), INTERVAL- 6 MONTH))
  GROUP BY MONTH(`datePaid`)
) as `t5`

你可以猜测这个查询实际上是有效的,但是我得到了20条奇数线,而不是得到6条线。我知道我必须以某种方式对它们进行分组,但我不确定如何?最大的问题是不同的表具有不同的命名日期字段,在此阶段我无法更改。

edit-- 我尝试过不同的方式。我已将查询减少到两个项目以使其更容易。如果我在它之间使用它,但我不得不以某种方式在不同的月份喂食。

SELECT `totalSales`,`totalPurchase`
FROM
(SELECT SUM((`Unit_Cost`*`ExchangeRate`)*`Quantity`) AS `totalSales` FROM `salesinvoice_products` AS `prod`
LEFT JOIN `salesinvoice` AS `inv` on `prod`.`SalesInvoice_id`=`inv`.`SalesInvoice_id`
WHERE `InvoiceDate` BETWEEN '2017-06-01' AND '2017-06-26'
) as `t1`,

(SELECT SUM((`InvoiceValue`*`ExchangeRate`)-`VATValue`) as `totalPurchase` FROM `purchaseinvoice`
WHERE `InvoiceDate` BETWEEN '2017-06-01' AND '2017-06-26'
) as `t2`

这样做是为了给我一个月的数据,但它对我的初始查询没有太多帮助,但是我尝试了其他的东西。

1 个答案:

答案 0 :(得分:0)

对,我有一个有效的答案,但无论如何它都不是最佳解决方案。我真的想用一个查询回答我的问题,但我想出的唯一解决方案是使用php并循环mysql。是的,我知道这不是理想的,但我需要快速找到解决方案,即使它不是最好的方法,它也可以。我会继续看看我是否可以用JUST mysql来解决这个问题,但目前我们必须这样做。

基本上我的查询一次只做一个月。所以我必须在php中增加月份并重新运行所有6个月的查询。

现在寻找更好的解决方案。

$plArray = array();
for ($i = 0; $i <= 5; $i++):
    $month  = date("m", strtotime( date( 'Y-m-01' )." -$i months"));
    $query="SELECT IFNULL(`totalSales`,0)-IFNULL(`totalPurchase`,0)-(IFNULL(`totalNetpay`,0)+IFNULL(`totalP32`,0)+IFNULL(`totalPension`,0)) as `totalPL`";
    $query.="
    FROM (SELECT SUM((`Unit_Cost`*`ExchangeRate`)*`Quantity`) AS `totalSales` FROM `salesinvoice_products` AS `prod`
    LEFT JOIN `salesinvoice` AS `inv` on `prod`.`SalesInvoice_id`=`inv`.`SalesInvoice_id`
    WHERE MONTH(`InvoiceDate`)=?
    ) as `t1`,

    (SELECT SUM((`InvoiceValue`*`ExchangeRate`)-`VATValue`) as `totalPurchase` FROM `purchaseinvoice`
    WHERE MONTH(`InvoiceDate`)=?
    ) as `t2`,

    (SELECT SUM(`NetAmountPaid`) as `totalNetpay` FROM `payroll_netpay`
    WHERE MONTH(`Date`)=?
    ) as `t3`,

    (SELECT SUM(`totalAmountDue`) as `totalPension` FROM `payroll_pensionrecords`
    WHERE MONTH(`datePaid`)=?
    ) as `t4`,

    (SELECT SUM(`totalAmountDue`) as `totalP32` FROM `payroll_p32records`
    WHERE MONTH(`datePaid`)=?
    ) as `t5`";
    $stmt = $this->logon->db->prepare($query);
    $stmt->bind_param('iiiii',$month,$month,$month,$month,$month);
    $stmt->execute();
    $result = $this->functions->get_result($stmt);
    $plArray[] = array(
    'date' => date("Y-m", strtotime( date( 'Y-m-01' )." -$i months")),
    'total' => $result[0]['totalPL']
    );
endfor;