将两个查询合并为一个而不使用UNION ALL

时间:2012-04-20 03:34:38

标签: php mysql

我有以下两个mysql查询,我正在尝试将它们合并为一个。

查询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']; ?>

请您帮我把两个问题合并成一个吗?

先谢谢:)

3 个答案:

答案 0 :(得分: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();

请参阅此处的编辑

How to create General Ledger/T-Account using PHP Mysql

由于@vyegorov

,这里是此查询的修改和优化版本

Strange Behaviour of Group by in Query which needs to be optimized

答案 1 :(得分:1)

为什么不使用两个表中的SELECT(debit_side和credit_side)?当然,每张桌子都需要所有的JOIN。

答案 2 :(得分:1)

为简单起见,我省略了transaction_info JOIN:

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'

此查询引用帐户表两次,一次是借方,一次是贷方。这允许您显示交易双方的信息。