sql >> Database teknologi >  >> RDS >> Mysql

Sådan opretter du hovedbog/T-konto ved hjælp af 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();
}

Beklager det sene svar, men dette er den perfekte ting, du vil teste, før du gør noget med. Jeg ønskede at oprette en visningsfil til dette, men det ser ud til at være kompliceret og tager mere tid, hvilket jeg i øjeblikket søer

REDIGERET

Denne løsning kan måske give dig udsigt, da jeg har testet den. Jeg har prøvet at kombinere disse to forespørgsler, og det er lykkedes. Forespørgslen kan ligne noget komplekst, men det giver de perfekte resultater. Her er det

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

Selvom denne forespørgsel fungerer fint, men her er en modificeret og optimeret version af denne forespørgsel. Jeg lærte virkelig ting af denne forespørgsel, du bør også tage et kig på dette

Mærkelig opførsel af gruppe af i forespørgsel, som skal optimeres




  1. Nummerseriegenerator-udfordringsløsninger – Del 4

  2. Udfyldning af html-formularer med mysql-data ved hjælp af php coming up null

  3. MySQL konverter YEARWEEK til dato

  4. SQL dataopdagelse og klassificering