简体   繁体   中英

codeigniter query: how to take all the record from a column in a table when i use sum Select function to get data from another table

the case is i have this table called journal_type
id_type | account_no | description |
---------------------------------------------
1 | 1.1 | bank
2 | 1.2 | bank & cash
3 | 1.3 | sale
4 | 1.4 | stock
5 | 1.5 | office tools
6 | 1.6 | payroll


and the second table called journal is something like this
id_transaction | id_type | debit | date_of_transaction
---------------------------------------------
1 | 1 | 50.00 | 08-22-2018
2 | 3 | 90.00 | 08-21-2018
3 | 1 | 80.00 | 08-17-2018

what i do so far is sum the data from second table's debit column based on month(in this case is august) and i want to group it by the id_type. so it would be something like id_type1 = 130.00 and id_type3 = 90.00
but i want the output is printing all the record from the id_type column in first table and let the other column null when it don't used. maybe the result i want is something like this

id_type | account_no | description | debit
---------------------------------------------
1 | 1.1 | bank | 130
2 | 1.2 | bank & cash| 0
3 | 1.3 | sale | 90
4 | 1.4 | stock | 0
5 | 1.5 | office tools |0
6 | 1.6 | payroll | 0

public function get_finance_debitkredit($periode1, $periode2){
  $query = $this->db->select('sum(debit) as debit, journal_type.account_no, journal_type.description, journal.id_type')
          ->from('journal_type')
          ->join('journal', 'journal.id_type = journal_type.id_type', 'left outer')
          ->group_by('journal.id_type')
          ->where('date_of_transaction >=', $periode1)
          ->where('date_of_transaction <=', $periode2)
          ->order_by('journal_type.account_no')
          ->get();
    return $query;  
}

but so far what I get is that it's only printed the record data that have the transaction (in this case is the id_type 1 and id_type_3) so only 2 row that appear. can i make all the 6 row of journal_type appear like what i want with some change of the query. that code is from my model in codeigniter

You first need to create seperate table for that records and then need to left join with that table. Use following query it will give the result you want

public function get_finance_debitkredit($periode1, $periode2){
$query = $this->db->select('finl_tbl.debit, journal_type.account_no, journal_type.description, journal_type.id_type')
      ->from('journal_type')
      ->join('(SELECT sum(debit) as debit,id_type FROM journal WHERE date_of_transaction >='.$periode1.' AND date_of_transaction <='.$periode2.' group by id_type) as finl_tbl', 'finl_tbl.id_type = journal_type.id_type', 'left')
      ->order_by('journal_type.account_no')
      ->get();
return $query;  
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM