从多个可搜索的表中计算余额(Codeigniter& Mysql)

时间:2016-02-13 18:00:52

标签: php mysql codeigniter

我希望计算系统下所有用户的总借记和总信用额之间的平衡,并且管理员允许通过PHP页面(使用CodeIgniter)根据日期范围进行搜索。

我目前有两张桌子:

TBL_MEMBER表


member_id   |   username
--------------------------
    1       |     John
    2       |     Alex
    3       |     Jane   

tbl_transactions


member_id   |   type    |   amount   |    created_date  
------------------------------------------------------
    1       |   debit   |     500    |     2016-01-25
    1       |   debit   |     500    |     2016-01-27
    1       |   credit  |     200    |     2016-01-27
    3       |   debit   |     100    |     2016-01-28
    1       |   credit  |     50     |     2016-01-29
    3       |   debit   |     500    |     2016-02-02
    3       |   credit  |     200    |     2016-02-03

我想按照以下

显示内容

tbl_transactions


username   |   Total Debit    |   Total Credit    |    Balance    
----------------------------------------------------------------
 John      |       1000       |        250        |      750   
 Jane      |        600       |        200        |      400   

该表不包括member_id = 2,因为他没有进行任何交易。

我很难将结果显示在总借记卡和&信用额度只应显示借方和贷方的总和。在搜索期内获得的信用,即 日期范围:2016-01-27至2016-01-29,结果应如下:


username   |   Total Debit    |   Total Credit    |    Balance    
----------------------------------------------------------------
 John      |        500       |        250        |      250   
 Jane      |        100       |         0         |      100   

以下是我使用CodeIgniter的模式:

public function get_member_history_record($search,$per_pg,$offset){
                $fill  = array((int)1);
                $temp  = array();
                $data  = array();

                $sQuery = "SELECT m.username,
    SUM(amount) WHERE t.type = 'debit' AS total_debit,
    SUM(amount) WHERE t.type = 'credit' AS total_credit,
    total_deposit-total_credit AS balance
    FROM
    tbl_transaction t
    INNER JOIN
    tbl_member m
    ON
    m.member_id = t.member_id
    GROUP BY
    m.username
        WHERE 1 = ?";

      if( strlen($search['date_from']) > 0  && strlen($search['date_to']) > 0  ){
                $sQuery .= ' AND date(t.date_create) BETWEEN ? ';
                array_push($fill,$search['date_from']);

                $sQuery .= ' AND ? ';
                array_push($fill,$search['date_to']);
            }else{
                if( strlen($search['date_from']) > 0 ){
                    $sQuery .= ' AND date(t.date_create) = ? ';
                    array_push($fill,$search['date_from']);
                }
                if( strlen($search['date_to']) > 0 ){
                    $sQuery .= ' AND date(t.date_create) = ? ';
                    array_push($fill,$search['date_to']);
                }
            }

                $sQuery .= "ORDER BY total_deposit DESC LIMIT ?,?";
                array_push($fill,(int)$offset,(int)$per_pg);

                $query  = $this->db->query($sQuery,$fill);
                //echo $this->db->last_query();
                if($query->num_rows() > 0){
                        foreach ($query->result() as $rows):
                        $temp['member_id']          = $rows->member_id;
                        $temp['username']           = $rows->username;  
                        $temp['created_date']       = $rows->created_date;
                        $temp['total_debit']        = $rows->total_debit;   
                        $temp['total_credit']       = $rows->total_credit;
                        $temp['balance']      = $this->balance;

                                array_push($data, $temp);
                        endforeach;
                }
                return $data;
        }

请帮忙!谢谢!

0 个答案:

没有答案