使用多个条件求和数组值

时间:2019-03-20 20:44:52

标签: php mysql

我正在尝试根据客户和日期范围获取数组中所有“数量”元素的总和。我希望输出数组(最终结果)看起来像这样……

Array
(
[0] => stdClass Object
    (
        [company_name] => Yeung's Architects
        [customer_id] => 3
        [name] => Billable Expenses Income
        [due_date] => 2019-04-19 00:00:00
        [amount] => 1543.64
        [account_id] => 75
    )

[1] => stdClass Object
    (
        [company_name] => Clement's Cleaners
        [customer_id] => 8
        [name] => Billable Expenses Income
        [due_date] => 2019-04-19 00:00:00
        [amount] => 2177.27
        [account_id] => 75
    )

[2] => stdClass Object
    (
        [company_name] => Clement's Cleaners
        [customer_id] => 8
        [name] => Billable Expenses Income
        [due_date] => 2019-05-19 00:00:00
        [amount] => 295.45
        [account_id] => 75
    )
 )

这是我正在使用的sql调用...

SELECT c.company_name
     , i.customer_id
     , a.name
     , i.due_date
     , li.amount
     , li.account_id 
  FROM invoices i 
  JOIN customers c 
    ON c.customer_id = i.customer_id
  JOIN invoices_line_items li 
    ON li.guid = i.guid 
   AND li.invoice_id = i.invoice_id
  JOIN accounts a 
    ON a.guid = li.guid 
   AND a.account_id = li.account_id
 WHERE i.guid = ? 
   AND i.date >= "'.$firstOfThisMonth.'" ',array($guid));

从sql调用开始生成的数组开始使用...

Array
(
[0] => stdClass Object
    (
        [company_name] => Yeung's Architects
        [customer_id] => 3
        [name] => Billable Expenses Income
        [due_date] => 2019-04-19 00:00:00
        [amount] => 1362.73
        [account_id] => 75
    )

[1] => stdClass Object
    (
        [company_name] => Yeung's Architects
        [customer_id] => 3
        [name] => Billable Expenses Income
        [due_date] => 2019-04-19 00:00:00
        [amount] => 180.91
        [account_id] => 75
    )

[2] => stdClass Object
    (
        [company_name] => Clement's Cleaners
        [customer_id] => 8
        [name] => Billable Expenses Income
        [due_date] => 2019-04-19 00:00:00
        [amount] => 177.27
        [account_id] => 75
    )

[3] => stdClass Object
    (
        [company_name] => Clement's Cleaners
        [customer_id] => 8
        [name] => Billable Expenses Income
        [due_date] => 2019-05-19 00:00:00
        [amount] => 159.09
        [account_id] => 75
    )

[4] => stdClass Object
    (
        [company_name] => Clement's Cleaners
        [customer_id] => 8
        [name] => Billable Expenses Income
        [due_date] => 2019-05-19 00:00:00
        [amount] => 136.36
        [account_id] => 75
    )

[5] => stdClass Object
    (
        [company_name] => Clement's Cleaners
        [customer_id] => 8
        [name] => Services
        [due_date] => 2019-04-19 00:00:00
        [amount] => 2000.00
        [account_id] => 1
    )

   )

我目前用于尝试根据客户ID和发票月份对所有金额求和的代码。

            $output = array($data[0]); $c = 1;
            foreach ($data as $key => $value) {
                if ($key > 0) {

                    if (in_array($value->customer_id,array_column($output, 'customer_id'))) {

                        $valueDate = substr($value->due_date,0,7);
                        foreach ($output as $k => $v) {
                            if ($v->customer_id == $value->customer_id) {
                                $vDate = substr($v->due_date,0,7);
                                if ($vDate == $valueDate) {
                                    $output[$k]->amount = $output[$k]->amount + $value->amount;
                                } else {
                                    $output[$c] = $value;
                                    $c++;
                                }

                            }
                        }

                    } else {
                        $output[$c] = $value;
                        $c++;
                    }

                }
            }

            return $output;

这是我从上述代码中得到的最终结果...

Array
(
[0] => stdClass Object
    (
        [company_name] => Yeung's Architects
        [customer_id] => 3
        [name] => Billable Expenses Income
        [due_date] => 2019-04-19 00:00:00
        [amount] => 1543.64
        [account_id] => 75
    )

[1] => stdClass Object
    (
        [company_name] => Clement's Cleaners
        [customer_id] => 8
        [name] => Billable Expenses Income
        [due_date] => 2019-04-19 00:00:00
        [amount] => 2177.27
        [account_id] => 75
    )

[2] => stdClass Object
    (
        [company_name] => Clement's Cleaners
        [customer_id] => 8
        [name] => Billable Expenses Income
        [due_date] => 2019-05-19 00:00:00
        [amount] => 295.45
        [account_id] => 75
    )

[3] => stdClass Object
    (
        [company_name] => Clement's Cleaners
        [customer_id] => 8
        [name] => Billable Expenses Income
        [due_date] => 2019-05-19 00:00:00
        [amount] => 136.36
        [account_id] => 75
    )

[4] => stdClass Object
    (
        [company_name] => Clement's Cleaners
        [customer_id] => 8
        [name] => Services
        [due_date] => 2019-04-19 00:00:00
        [amount] => 2000.00
        [account_id] => 1
    )

[5] => stdClass Object
    (
        [company_name] => Clement's Cleaners
        [customer_id] => 8
        [name] => Services
        [due_date] => 2019-04-19 00:00:00
        [amount] => 2000.00
        [account_id] => 1
    )

   )

前3个键是正确的结果,但是随后添加了3个额外的键,我不确定它们是如何添加的。我的大脑现在坏了,请有人帮我,任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:1)

所以,如果我要关注的是,您只想按company_name进行分组,或者还有其他维度?实际上,在SQL中执行此操作非常简单。

使用聚合函数,总和和按公司字段分组进行查询。您可以在下面看到很少的更改。

SELECT c.company_name
     , i.customer_id
     , a.name
     , i.due_date
     , SUM(li.amount)
     , AVG(li.amount)
     , li.account_id 
  FROM invoices i 
  JOIN customers c 
    ON c.customer_id = i.customer_id
  JOIN invoices_line_items li 
    ON li.guid = i.guid 
   AND li.invoice_id = i.invoice_id
  JOIN accounts a 
    ON a.guid = li.guid 
   AND a.account_id = li.account_id
 WHERE i.guid = ? 
   AND i.date BETWEEN :startDate AND :endDate
 GROUP BY i.customer_id

更多信息可在此处找到:https://dev.mysql.com/doc/refman/8.0/en/group-by-functions.html和此处https://dev.mysql.com/doc/refman/5.7/en/group-by-handling.html

如果需要,您可以添加第二个GROUP BY(实际需要的数量)。假设您要查询一个“按月按客户求和”的查询。你会做类似...

 GROUP BY i.customer_id, DATE_FORMAT(i.due_date, '%Y-%m')

WITH ROLLUP在调试中很有用。