简体   繁体   English

php数组的总和(从mysql结果创建),具体取决于另一个mysql列中的mysql值

[英]Sum php array (created from mysql results) depending on mysql values in another mysql column

One table called 18_7_ChartOfAccounts looks like this: 一个名为18_7_ChartOfAccounts表如下所示:

ID | AccountNumber
-------------
1  | 2310
2  | 2380
3  | 2610

Another table called 2_1_journal looks like this: 另一个名为2_1_journal表如下所示:

ID | Amount | DebitAccount
--------------------------
1  | 26.03  | 2310
2  | 200.00 | 2310
3  | 3.63   | 2380
4  | 119.83 | 2380
5  | 33.86  | 2610
6  | 428.25 | 2610

Aim is to get results that looks like this: 目的是获得如下结果:

DebitAccount 2310 total is: 226.03
DebitAccount 2380 total is: 123.46
DebitAccount 2310 total is: 462.11

226.03 in this example is total of 26.03 + 200.00 在此示例中,226.03总计为26.03 + 200.00

At first mysql code 起初mysql代码

$query = "SELECT j.Amount, j.DebitAccount FROM 18_7_ChartOfAccounts AS c LEFT JOIN 2_1_journal AS j ON (c.AccountNumber = j.DebitAccount)";
$sql = $db->prepare($query);
$sql->execute();
$data = $sql->fetchAll(PDO::FETCH_ASSOC);

With print_r($data); 使用print_r($data); get long list of arrays like 得到像这样的长数组列表

[31] => Array
    (
        [Amount] => 26.03
        [DebitAccount] => 2310

[32] => Array
    (
        [Amount] => 200.00
        [DebitAccount] => 2310

If in mysql query use SUM(j.Amount) then get only one total amount (suppose total amount of Column Amount ). 如果在mysql查询中使用SUM(j.Amount)则仅获得一个总数(假设Column Amount总数)。

With

foreach($data as $result){
if(strlen($result['Amount']) > 0 ) {
echo "Amount ". $result['Amount']. "Account name ". $result['DebitAccount']. "<br>";
print_r (array_sum($result));
}
}

Get something like this 得到这样的东西

Amount 123.97Account name 2310
2433.97Amount 26.03Account name 2310
2336.03Amount 200.00Account name 2310

Any ideas how to get necessary results (marked bold)? 有任何想法如何获得必要的结果(以粗体显示)吗?

Update 更新资料

Changed $query to 将$ query更改为

$query = "SELECT SUM(j.Amount), j.DebitAccount FROM 18_7_ChartOfAccounts AS c LEFT JOIN 2_1_journal AS j ON (c.AccountNumber = j.DebitAccount) group by j.DebitAccount";

with print_r($data); print_r($data); get array like this 得到这样的数组

Array
(
[0] => Array
    (
        [SUM(j.Amount)] => 
        [DebitAccount] => 
    )

[1] => Array
    (
        [SUM(j.Amount)] => 110900.16
        [DebitAccount] => 2310
    )

[2] => Array
    (
        [SUM(j.Amount)] => 3660.86
        [DebitAccount] => 2380
    )

With array seems all works. 与数组似乎所有的作品。 Now with foreach changed to echo "Amount ". $result['SUM(j.Amount)']. " Account name ". $result['DebitAccount']. "<br>"; 现在,将foreach更改为echo "Amount ". $result['SUM(j.Amount)']. " Account name ". $result['DebitAccount']. "<br>"; echo "Amount ". $result['SUM(j.Amount)']. " Account name ". $result['DebitAccount']. "<br>";

Get 得到

Amount 110900.16 Account name 2310
Amount 3660.86 Account name 2380
Amount 85247.40 Account name 2610

Seems also ok. 似乎还可以。 Thanks 谢谢

You are going about it wrong. 您正在做错事。 You can get the sum through MySql statement itself. 您可以通过MySql语句本身获取总和。

Use the aggrgate function sum along with group by clause. 将aggrgate函数sum和group by子句一起使用。

Like this, 像这样,

SELECT DebitAccount,sum(Account) from  2_1_journal group by DebitAccount

Your full code: 您的完整代码:

$query = " SELECT DebitAccount,sum(Account) as Total from  2_1_journal group by DebitAccount";
$sql = $db->prepare($query);
$sql->execute();
$data = $sql->fetchAll(PDO::FETCH_ASSOC);
foreach($data as $result){
if(strlen($result['Total']) > 0 ) {
echo "DebitAccount ". $result['DebitAccount']. "Total is: ". $result['Total']. "<br>";
print_r (array_sum($result));
}
}
SELECT DebitAccount, SUM(Amount) 
FROM 2_1_journal 
GROUP BY DebitAccount

您必须在查询中使用GROUP BY

SELECT DebitAccount, SUM(Amount) AS Amount FROM 2_1_journal GROUP BY DebitAccount

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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