我有以下两个 mysql 查询,我正在尝试将它们合并为一个。
查询一:
$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']; ?>
请您帮我把这两个查询合二为一吗?
提前致谢 :)