2012-04-20 116 views
3

我有以下兩個mysql查詢,我試圖將它們合併爲一個。將兩個查詢合併爲一個而不使用UNION ALL

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

請您幫助我把兩個查詢合併爲一個?

感謝提前:)

回答

5

我已經聯合這些queryies

 $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

+0

你檢查了答案嗎? – 2012-04-23 11:17:31

+0

我不能夠感謝你。它的工作現在雖然沒有顯示一個信息,但我認爲我可以從這裏處理它。我也必須在我的視圖文件中進行一些調整。你是一個了不起的開發者,你已經幫了我很多了,謝謝你。 – 2012-04-23 11:18:13

+0

它花了我近三天,並通過debit_side.account_code是我發現的最重要的事情 – 2012-04-23 11:20:12

1

你爲什麼不使用兩個表(debit_side和credit_side)SELECT?當然,每個表格都需要所有JOIN。

+0

你是想說我應該這樣做 - >> SELECT * FROM((query1)AND(query2))??我試過這個,但是顯示錯誤。你能否向我展示你的意思?謝謝:) – 2012-04-20 03:54:39

+0

如果你嘗試什麼'SELECT *,accounts.name AS DebitAccountName,debit_side.amount AS DebitAmount,credit_side.amount AS CreditAmount 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帳戶ON accounts.code = credit_side.account_code WHERE debit_side.account_code ='1001'' – temni 2012-04-20 04:04:31

+0

感謝Temni的回覆。我試過你的代碼,但它顯示$ row ['DebitAccountName']的值; AND $ row ['DebitAmount']; 但不是$ row ['CreditAccountName']; AND $ row ['CreditAmount']; – 2012-04-20 04:10:06

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' 

此查詢引用了賬表兩次,一次爲借方一次貸方。這使您可以顯示交易雙方的信息。