Mysql
 sql >> база данни >  >> RDS >> Mysql

Как да създадете Главна книга/Т-сметка с PHP Mysql

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

Въпреки че тази заявка работи добре, но ето една модифицирана и оптимизирана версия на тази заявка, аз наистина научих неща от тази заявка, трябва да погледнете и това

Странно поведение на група от в заявка, която трябва да бъде оптимизирана




  1. Database
  2.   
  3. Mysql
  4.   
  5. Oracle
  6.   
  7. Sqlserver
  8.   
  9. PostgreSQL
  10.   
  11. Access
  12.   
  13. SQLite
  14.   
  15. MariaDB
  1. Правилно използване на BoneCP

  2. Вземете име на колона, което има максимална стойност в ред sql

  3. Инсталация на Neo4j

  4. Java, как да променя текущата база данни с друга?

  5. Деактивирайте ONLY_FULL_GROUP_BY