我有以下兩個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']; ?>
請您幫助我把兩個查詢合併爲一個?
感謝提前:)
你檢查了答案嗎? – 2012-04-23 11:17:31
我不能夠感謝你。它的工作現在雖然沒有顯示一個信息,但我認爲我可以從這裏處理它。我也必須在我的視圖文件中進行一些調整。你是一個了不起的開發者,你已經幫了我很多了,謝謝你。 – 2012-04-23 11:18:13
它花了我近三天,並通過debit_side.account_code是我發現的最重要的事情 – 2012-04-23 11:20:12