![](/img/trans.png)
[英]How to get values from another table in codeigniter using the Active Record?
[英]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
情況是我有一個名為journal_type的表
id_type | account_no | 描述|
---------------------------------------------
1 | 1.1 | 銀行
2 | 1.2 | 銀行和現金
3 | 1.3 | 銷售
4 | 1.4 | 股票
5 | 1.5 | 辦公工具
6 | 1.6 | 工資單
第二個表叫做journal是這樣的
id_transaction | id_type | 借方 date_of_transaction
---------------------------------------------
1 | 1 | 50.00 | 2018年8月22日
2 | 3 | 90.00 | 2018/08/21
3 | 1 | 80.00 | 2018/08/17
到目前為止,我所做的是基於月份對第二張表的借方列中的數據求和(在本例中為8月),我想按id_type將其分組。 所以就像id_type1 = 130.00和id_type3 = 90.00
但我希望輸出從第一個表的id_type列中打印所有記錄,並在不使用時讓另一列為null。 也許我想要的結果是這樣的
id_type | account_no | 描述| 借方
---------------------------------------------
1 | 1.1 | 銀行 130
2 | 1.2 | 銀行和現金| 0
3 | 1.3 | 銷售 90
4 | 1.4 | 庫存 0
5 | 1.5 | 辦公工具| 0
6 | 1.6 | 工資單| 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;
}
但是到目前為止,我得到的是它僅打印了具有事務的記錄數據(在本例中為id_type 1和id_type_3),因此僅出現2行。 我可以通過查詢的某些更改使journal_type的所有6行顯示為我想要的樣子嗎? 該代碼來自我在codeigniter中的模型
您首先需要為該記錄創建一個單獨的表,然后需要與該表保持連接。 使用以下查詢,它將給出您想要的結果
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;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.