2

我正在尝试创建一个复式财务会计系统。我已经完成了它的数据库设计,请查看此链接以查看我的数据库图表。http://i39.tinypic.com/juhbv6.png

我已经尝试了很多来开发 mysql 查询以生成总帐,但我没有接近。我所做的只是为一个帐户创建分类帐,我必须为无限帐户创建。

我尝试过的查询如下:

 $this->db->select('*');
    $this->db->from('credit_side');
    $this->db->join('debit_side', ' debit_side.transaction_id_dr = credit_side.transaction_id_cr ');
    $this->db->join('transaction_info', 'transaction_info.transaction_id = credit_side.transaction_id_cr ');
    $this->db->join('accounts', 'accounts.code = credit_side.account_code ');
    $this->db->where('debit_side.account_code', '1001'); 
    $this->db->order_by('voucher_date','ASC');

在未能编写可以为所有帐户生成分类帐的 mysql 查询后,我写下了创建总帐/T 帐户的逻辑。

现在,你能帮我查询一下mysql吗?

请在执行以下操作之前检查数据库。提前致谢 :)

  1. 从帐户中获取accounts.nameAS AccountHead accounts.code

  2. 转到表格debit_sideget debit_side.account_code如果debit_side.account_code=accounts.code则获取credit_side.account_codeAS AccountName1(但是当我将在 php 中回显时,我想获取名称而不是代码本身)和credit_side.amountAS Amount1,SUM(credit_side.amount)AS TotalAmount1 来自credit_sidewhere debit_side.transaction_id_dr=credit_side.transaction_id_cr 并且transaction_info.voucher_date在 date1 和 date2 之间transaction_info.transaction_id=debit_side.transaction_id_dr

  3. 完成第二步后,转到表中credit_sideget credit_side.account_code如果
    = credit_side.account_codeaccounts.code则获取debit_side.account_codeAS AccountName2(但是当我将在 php 中回显时,我想获取名称而不是代码本身)和debit_side.amountAS Amount2,SUM(debit_side.amount)AS TotalAmount2 从debit_sidewhere credit_side.transaction_id_cr=debit_side.transaction_id_drtransaction_info.voucher_date在在 date1 和 date2 之间 WHERE transaction_info.transaction_id=credit_side.transaction_id_cr

现在在视图文件中,我的目标是:

   <table width="200" border="0">
     <tr><td colspan="5">Account Head <?echo $AccountHead ; ?> </td> </tr>


    <tr>
           <td>Dr.</td>
           <td>amount.</td>
            <td>&nbsp;</td>
           <td>Cr</td>
          <td>Amount</td>

    </tr>
     <tr>
       <td><?echo $AccountName1 ; ?></td>
       <td><?echo $Ammount1 ; ?></td>
      <td></td>
 <td><?echo $AccountName2 ; ?></td>
     <td><?echo $Ammount2 ; ?></td>

     </tr>

     <tr>
     <td>Total</td>
     <td><?echo $TotalAmount1 ; ?></td>
     <td>&nbsp;</td>
 <td>Total  </td>
 <td><?echo $TotalAmount2 ; ?></td>

     </tr>
     </table>

总帐样本

在此处输入图像描述

4

1 回答 1

5
public function getDebits(){    
    $data   =   array(
                'debit_side.account_code DebitCode',
                'group_concat(distinct accounts.name) as DebitAccount',
                'group_concat(debit_side.amount) as DebitAmount',
                'group_concat(transaction_info.voucher_date) as DebitVoucherDate'
                );
    $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();
}


public  function getCredits()
{
    $data   =   array(
                'credit_side.account_code CreditCode',
                'group_concat(distinct accounts.name) as CreditAccount',
                'group_concat(credit_side.amount) as CreditAmount',
                'group_concat(transaction_info.voucher_date) as CreditVoucherDate'
                );
    $this->db->select($data);
    $this->db->from('accounts');
    $this->db->join('debit_side','accounts.code = debit_side.account_code','left');
    $this->db->join('credit_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('credit_side.account_code');
    $this->db->order_by('credit_side.account_code','ASC');
    $query  =   $this->db->get();
    return  $query->result_array();
}

抱歉回复晚了,但这是您在做任何事情之前想要测试的完美东西。我想为此创建一个视图文件,但它似乎很复杂并且需要更多时间,目前我正在湖

已编辑

这个解决方案可能会在我测试过它时让你在视图上苦苦挣扎我曾经尝试过结合这两个查询并成功了。该查询可能看起来很复杂,但它正在获取完美的结果。这里是

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

虽然这个查询工作正常,但这里是这个查询的修改和优化版本我真的从这个查询中学到了东西你也应该看看这个

Group by 在查询中的奇怪行为需要优化

于 2012-04-17T14:37:13.733 回答