3

私は次の2つのmysqlクエリを持っており、それらを1つに結合しようとしています。

クエリ1:

$getData = $this->db->query("SELECT *,accounts.name AS DebitAccountName ,debit_side.amount AS DebitAmount
    FROM credit_side
    LEFT JOIN debit_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
    LEFT JOIN accounts ON accounts.code = credit_side.account_code 
    WHERE debit_side.account_code='1001'");

クエリ2:

 $getData = $this->db->query(SELECT *,accounts.name AS CreditAccountName,credit_side.amount AS CreditAmount
    FROM debit_side
    LEFT JOIN credit_side ON debit_side.transaction_id_dr = credit_side.transaction_id_cr
    LEFT JOIN transaction_info ON transaction_info.transaction_id = debit_side.transaction_id_dr
    LEFT JOIN accounts ON accounts.code = debit_side.account_code 
    WHERE credit_side.account_code='1001'");

UNION ALLを試しましたが、この場合は実際には機能しません。問題は、エコー$DebitAccountNameすると、結果も表示されること$CreditAccountNameです。これは望ましくありません。

私はCodeigniterを使用しており、ビューファイルではこのような結果をエコーすることを目指しています。

 <?php if(count($records) > 0) { ?>

<?php foreach ($records as $row){ ?>

<?php echo $row['DebitAccountName']; ?> 
  <?php echo $row['DebitAmount']; ?>

<?php echo $row['CreditAccountName']; ?> 
  <?php echo $row['CreditAmount']; ?>

2つのクエリを1つにまとめるのを手伝っていただけませんか。

前もって感謝します :)

4

3 に答える 3

5

私はこれらのクエリを組み合わせました

        $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();

こちらの編集もご覧ください

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

@vyegorovのおかげで、このクエリの修正および最適化されたバージョンがここにあります

最適化する必要があるQueryでのGroupbyの奇妙な動作

于 2012-04-23T09:14:48.237 に答える
1

両方のテーブル(debit_sideとcredit_side)からSELECTを使用してみませんか?もちろん、各テーブルに必要なすべてのJOINを使用します。

于 2012-04-20T03:44:57.670 に答える
1

簡単にするために、transaction_infoJOINは省略しました。

SELECT debacc.name AS DebitAccountName, debit_side.amount AS DebitAmount,
    credacc.name AS CreditAccountName, credit_side.amount AS CreditAmount
FROM debit_side
LEFT JOIN credit_side ON debit_side.transaction_id_dr = credit_side.transaction_id_cr
LEFT JOIN accounts credacc ON credacc.code = credit_side.account_code
LEFT JOIN accounts debacc ON debacc.code = debit_side.account_code
WHERE debit_side.account_code='1001'

このクエリは、アカウントテーブルを2回参照します。1回は借方側、もう1回は貸方側です。これにより、トランザクションの両側からの情報を表示できます。

于 2012-04-20T04:19:13.243 に答える