sql >> データベース >  >> RDS >> Mysql

PHPMysqlを使用して総勘定元帳/T-Accountを作成する方法

    public function getDebits(){    
        $data   =   array(
                    'debit_side.account_code DebitCode',
                    'group_concat(distinct accounts.name) as DebitAccount',
                    'group_concat(debit_side.amount) as DebitAmount',
                    'group_concat(transaction_info.voucher_date) as DebitVoucherDate'
                    );
        $this->db->select($data);
        $this->db->from('accounts');
        $this->db->join('credit_side','accounts.code = credit_side.account_code','left');
        $this->db->join('debit_side','debit_side.transaction_id_dr = credit_side.transaction_id_cr','left');
        $this->db->join('transaction_info','transaction_info.transaction_id = credit_side.transaction_id_cr','left');
        $this->db->group_by('debit_side.account_code');
        $this->db->order_by('debit_side.account_code','ASC');
        $query  =   $this->db->get();
        return  $query->result_array();
    }
    
    
    public  function getCredits()
    {
        $data   =   array(
                    'credit_side.account_code CreditCode',
                    'group_concat(distinct accounts.name) as CreditAccount',
                    'group_concat(credit_side.amount) as CreditAmount',
                    'group_concat(transaction_info.voucher_date) as CreditVoucherDate'
                    );
        $this->db->select($data);
        $this->db->from('accounts');
        $this->db->join('debit_side','accounts.code = debit_side.account_code','left');
        $this->db->join('credit_side','debit_side.transaction_id_dr = credit_side.transaction_id_cr','left');
        $this->db->join('transaction_info','transaction_info.transaction_id = credit_side.transaction_id_cr','left');
        $this->db->group_by('credit_side.account_code');
        $this->db->order_by('credit_side.account_code','ASC');
        $query  =   $this->db->get();
        return  $query->result_array();
    }
    

    返信が遅くなって申し訳ありませんが、これは何かをする前にテストしたい完璧なものです。このためのビューファイルを作成したかったのですが、複雑で時間がかかるようで、現時点では湖にいます

    編集済み

    このソリューションは、私がテストしたので、あなたを悩ませているかもしれません。しかし、これら2つのクエリを組み合わせようとして、成功しました。クエリは複雑に見えるかもしれませんが、完璧な結果を取得しています。ここにあります

    $data   =   array(
                      'debit_side.account_code    Code',
                      'group_concat(distinct accounts.name) as DebitAccount',
                      'group_concat(debit_side.amount) as DebitAmount',
                      'group_concat(transaction_info.voucher_date) as DebitVoucherDate',
                      '(SELECT group_concat(distinct accounts.name) as CreditAccount FROM (accounts)
                        left JOIN debit_side ON accounts.code = debit_side.account_code
                        left JOIN credit_side ON debit_side.transaction_id_dr = credit_side.transaction_id_cr
                        left JOIN transaction_info ON transaction_info.transaction_id = credit_side.transaction_id_cr
                        group by credit_side.account_code
                        having credit_side.account_code = `Code`) as CreditAccount',
                      '(SELECT  group_concat(credit_side.amount) as CreditAmount FROM (accounts)
                         left JOIN debit_side ON accounts.code = debit_side.account_code
                         left JOIN credit_side ON debit_side.transaction_id_dr = credit_side.transaction_id_cr
                         left JOIN transaction_info ON transaction_info.transaction_id = credit_side.transaction_id_cr
                       group by credit_side.account_code
                       having credit_side.account_code = `Code`) as CreditAmount',  
                      '(SELECT  group_concat(transaction_info.voucher_date) as CreditVoucherDate FROM (accounts)
                        left JOIN debit_side ON accounts.code = debit_side.account_code
                        left JOIN credit_side ON debit_side.transaction_id_dr = credit_side.transaction_id_cr
                        left JOIN transaction_info ON transaction_info.transaction_id = credit_side.transaction_id_cr
                        group by credit_side.account_code
                        having credit_side.account_code = `Code`) as CreditVoucherDate'
    
                    );
        $this->db->select($data);
        $this->db->from('accounts');
        $this->db->join('credit_side','accounts.code = credit_side.account_code','left');
        $this->db->join('debit_side','debit_side.transaction_id_dr = credit_side.transaction_id_cr','left');
        $this->db->join('transaction_info','transaction_info.transaction_id = credit_side.transaction_id_cr','left');
        $this->db->group_by('debit_side.account_code');
        $this->db->order_by('debit_side.account_code','ASC');
        $query  =   $this->db->get();
        return  $query->result_array();
    

    このクエリは正常に機能しますが、これはこのクエリの修正および最適化されたバージョンですが、このクエリから実際に学んだことは、これも確認する必要があります

    最適化が必要なクエリ内のGroupbyの奇妙な動作




    1. dd-mm-yyyy形式のsqlitedbから2つの日付の間のデータを選択するにはどうすればよいですか?

    2. SQLで最上位の親を見つける

    3. ウィンドウ関数または共通テーブル式:範囲内の前の行をカウントします

    4. アクセスから最も恩恵を受けるのはどの業界ですか?