简体   繁体   English

Codeigniter查询:当我使用sum Select函数从另一个表中获取数据时,如何从表中的列中获取所有记录

[英]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.

相关问题 如何使用Active Record从Codeigniter中的另一个表中获取值? - How to get values from another table in codeigniter using the Active Record? 如何在PHP Codeigniter中从一个表中获取特定列,并从另一表中获取所有其他数据 - How to get specific column from one table and all other data from other table in php codeigniter 从选择查询中的另一个表中全选 - Select all from another table in select query 如何从一个表中选择数据并从另一表中获取所有相关记录? - How to select data from one table and get all associated records from another table? 如何从一个表中选择与表中的列值相匹配的数据? - How do I select data from one table where column values from that table match the conatenated column values of another table? 从一个条件为的表中检索数据,并在一个SELECT查询中检查另一个表中的记录 - Retrieve data from a table with a condition where and check the record in another table in one SELECT query 无法从选择查询中的表中获取所有数据 - can't get all data from table in select query 使用codeigniter从两个表中获取记录,其中所有匹配的记录都来自第一个表 - Get Record from two table using codeigniter where all matching record from first table 如何从一个表中获取不同的数据,然后联接另一个表并取2列之和 - how to get the distinct data from a table and then join other table and take sum of 2 columns 从一个表中选择数据,按另一表中的数据总和排列 - Select data from one table, arrange by a sum of data from another
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM