使用php获取分类帐报表mysql表

mcvgt66p  于 2021-06-17  发布在  Mysql
关注(0)|答案(1)|浏览(255)

关闭。这个问题需要更加突出重点。它目前不接受答案。
**想改进这个问题吗?**通过编辑这篇文章更新这个问题,使它只关注一个问题。

两年前关门了。
改进这个问题
我有一张table
这是我的table

ID |VoucherNO|   VoucherType|   AccName         |   Particulars       |   Debit     |    Credit |     Date
---|---------|--------------|-------------------|---------------------|-------------|-----------|------------
1  |    1    |  Cash Payment|    OFFICE EXPENSE |   CASH ACCOUNT      |     500     |     0 | 2018-nov-25 
---|---------|--------------|-------------------|---------------------|-------------|-----------|------------
2  |    1    |  Cash Payment|    CASH ACCOUNT   |   OFFICE EXPENSE    |     0       |     500   | 2018-nov-25 
---|---------|--------------|-------------------|---------------------|-------------|-----------|------------
3  |    2    |  Cash Payment|    OFFICE EXPENSE |   CASH ACCOUNT      |     250     |     0 | 2018-nov-26 
---|---------|--------------|-------------------|---------------------|-------------|-----------|------------
4  |    2    |  Cash Payment|    CASH ACCOUNT   |   OFFICE EXPENSE    |     0       |     250   | 2018-nov-26 
---|---------|--------------|-------------------|---------------------|-------------|-----------|-----------
5  |    3    |  Cash Payment|    OFFICE EXPENSE |   CASH ACCOUNT      |     100     |     0 | 2018-nov-27 
---|---------|--------------|-------------------|---------------------|-------------|-----------|------------
6  |    3    |  Cash Payment|    CASH ACCOUNT   |   OFFICE EXPENSE    |     0       |     100   | 2018-nov-27 
-------------------------------------------------------------------------------------------------------------

我想要这样的表

----------------------------------------------------------------------------------------- ---
Date         | VoucherNo |    VoucherType   | Partuclars    | debit  |   credit   |  Balance
---------------------------------------------------------------------------------------------

2018-nov-25      1          Cash Payment     CASH ACCOUNT   500                      500
___________________________________________________________________________________________
2018-nov-26      2          Cash Payment     CASH ACCOUNT   250                      750                
___________________________________________________________________________________________
2018-nov-27      3          Cash Payment     CASH ACCOUNT   100                      850
____________________________________________________________________________________________

                            total                          850            0.00      850

这个表是从清单软件接收到我的,但是我不知道如何用php和mysql完成这个报表查询

j2qf4p5b

j2qf4p5b1#

php可能要完成大部分工作,所以我将省略sql,但我假设一个简单的 SELECT * FROM myTableName ORDER BY VoucherNo; 会起作用(如果主键是order by,则可能不需要order by)。我也使用0作为起始平衡,所以调整,如果你需要的话。

$balance = 0;
$curdate = '';
$output = array();
$curValue = array(
    'Date' => '',
    'VoucherNo' => 0, 
    'VoucherType' => '', 
    'Partuclars' => '', 
    'debit' => 0, 
    'credit' => 0, 
    'Balance' => 0
);

foreach($prePopulatedResultsArray as $row) {

    // if the current day is done being populated, add data to output and reset current
    if ($curdate !== $row['Date']) {
    // only add the current value if it has been populated
        if ($curdate !== '')
            $output[] = $curValue;

        $curValue = array(
            'Date' => $row['Date'], 
            'VoucherNo' => $row['VoucherNo'], 
            'VoucherType' => $row['VoucherType'], 
            'Partuclars' => $row['Partuclars'], 
            'debit' => $row['debit'], 
            'credit' => $row['credit'], 
            'Balance' => $balance + $row['debit']
        );
        $curdate = $row['Date'];

    // if it is still the same day as the last iteration, add to existing values
    } else {
        $curValue['debit'] = $curValue['debit']+$row['debit'];
        $curValue['credit'] = $curValue['debit']+$row['credit'];
        $balance = $balance + $row['debit'];
        $curValue['Balance'] = $curValue['Balance']+$balance;
    }
}

这可能有点偏离你的目标,因为我是根据你的输出的事实,你正在添加 debitBalance 我正在填充 credit ,即使预期结果中的值为空。如果答案需要调整,请告诉我。

相关问题