[英]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 情况是我有一个名为journal_type的表
id_type | id_type | account_no |
account_no | description |
描述|
--------------------------------------------- ---------------------------------------------
1 | 1 | 1.1 |
1.1 | bank
银行
2 | 2 | 1.2 |
1.2 | bank & cash
银行和现金
3 | 3 | 1.3 |
1.3 | sale
销售
4 | 4 | 1.4 |
1.4 | stock
股票
5 | 5 | 1.5 |
1.5 | office tools
办公工具
6 | 6 | 1.6 |
1.6 | payroll
工资单
and the second table called journal is something like this 第二个表叫做journal是这样的
id_transaction | id_transaction | id_type |
id_type | debit |
借方 date_of_transaction
date_of_transaction
--------------------------------------------- ---------------------------------------------
1 | 1 | 1 |
1 | 50.00 |
50.00 | 08-22-2018
2018年8月22日
2 | 2 | 3 |
3 | 90.00 |
90.00 | 08-21-2018
2018/08/21
3 | 3 | 1 |
1 | 80.00 |
80.00 | 08-17-2018
2018/08/17
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. 到目前为止,我所做的是基于月份对第二张表的借方列中的数据求和(在本例中为8月),我想按id_type将其分组。 so it would be something like id_type1 = 130.00 and id_type3 = 90.00
所以就像id_type1 = 130.00和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. 但我希望输出从第一个表的id_type列中打印所有记录,并在不使用时让另一列为null。 maybe the result i want is something like this
也许我想要的结果是这样的
id_type | id_type | account_no |
account_no | description |
描述| debit
借方
--------------------------------------------- ---------------------------------------------
1 | 1 | 1.1 |
1.1 | bank |
银行 130
130
2 | 2 | 1.2 |
1.2 | bank & cash|
银行和现金| 0
0
3 | 3 | 1.3 |
1.3 | sale |
销售 90
90
4 | 4 | 1.4 |
1.4 | stock |
库存 0
0
5 | 5 | 1.5 |
1.5 | office tools |0
办公工具| 0
6 | 6 | 1.6 |
1.6 | payroll |
工资单| 0
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. 但是到目前为止,我得到的是它仅打印了具有事务的记录数据(在本例中为id_type 1和id_type_3),因此仅出现2行。 can i make all the 6 row of journal_type appear like what i want with some change of the query.
我可以通过查询的某些更改使journal_type的所有6行显示为我想要的样子吗? that code is from my model in codeigniter
该代码来自我在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;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.