【问题标题】: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 tablecodeigniter 查询:当我使用 sum Select 函数从另一个表中获取数据时,如何从表中的列中获取所有记录
【发布时间】:2019-01-14 15:04:29
【问题描述】:

情况是我有这张名为 journal_type 的表
id_type |帐户号 |描述 |
---------------------------------------------
1           | 1.1               | 银行
2           | 1.2               | 银行和现金
3           | 1.3               | 出售
4           | 1.4               | 库存
5           | 1.5               | 办公工具
6           | 1.6               |工资单


第二个名为 journal 的表是这样的
id_transaction | id_type |借方|交易日期
---------------------------------------------
1                     | 1        | 50.00 | 08-22-2018
2                     | 3        | 90.00 | 08-21-2018
3                      | 1        | 80.00 | 08-17-2018

到目前为止,我所做的是根据月份(在本例中为 8 月)对第二个表的借方列中的数据求和,并且我想按 id_type 对其进行分组。所以它会像 id_type1 = 130.00 和 id_type3 = 90.00
但我希望输出打印第一个表中 id_type 列中的所有记录,并在不使用时让另一列为空。也许我想要的结果是这样的

id_type |帐户号 |描述 |借记
---------------------------------------------
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 行。我可以通过更改查询使所有 6 行的 journal_type 看起来像我想要的那样吗? 该代码来自我在 codeigniter 中的模型

【问题讨论】:

    标签: php mysql codeigniter join


    【解决方案1】:

    您首先需要为该记录创建单独的表,然后需要与该表进行左连接。使用以下查询它会给出你想要的结果

    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;  
    }
    

    【讨论】:

      猜你喜欢
      • 2022-09-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-08
      • 1970-01-01
      • 1970-01-01
      • 2019-05-02
      相关资源
      最近更新 更多