【问题标题】:Group by query not working fine According me按查询分组不能正常工作根据我
【发布时间】:2020-04-15 08:25:26
【问题描述】:

我想得到一个值 group_by 'recivedID' 但不能正常工作。查找值只有一个第一个值显示在表单上如何显示所有添加所有值并显示。

表'fuel_transaction'

recivedID hsdltrs
      1     500
      1     600
      1     300
      2     100
      2     600 

结果显示在表单上 // 不需要

recivedID ->1 
hsdltrs ->500  //worng result

我需要结果

//hsdltrs ->500+600+300=1400

    recivedID ->1 
    hsdltrs ->1400


 <?php
      //$this->db->Select SUM(ABS(hsdltrs)) as Data ; //error please check its sir                                
      $this->db->where('issuedate >=',$date1);
      $this->db->where('issuedate <=',$date2);
      $this->db->where('recivedID',$a); 
      $this->db->group_by('recivedID');
      $queryb = $this->db->get('fuel_transaction'); 
      if ($queryb->num_rows() > 0)
              {
                foreach ($queryb->result() as $rowc)
                    {
                    $tdate = $rowc->issuedate;
                    $hsdltrs = $rowc->hsdltrs;
                    $hsd += $rowc->hsdltrs;
                ?>

【问题讨论】:

    标签: php sql group-by


    【解决方案1】:

    您需要添加select_sum。 然后,在您的foreach 中,可以使用$rowc-&gt;sumhsd

    $this->db->select_sum('hsdltrs', 'sumhsd'); //Here it comes                               
    $this->db->where('issuedate >=',$date1);
    $this->db->where('issuedate <=',$date2);
    $this->db->where('recivedID',$a); 
    $this->db->group_by('recivedID');
    $queryb = $this->db->get('fuel_transaction'); 
    
    if ($queryb->num_rows() > 0) {
        foreach ($queryb->result() as $rowc) {
            $tdate = $rowc->issuedate;           
            $hsdltrs = $rowc->sumhsd; //Here you get the sum
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-09-29
      • 2017-06-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-05
      • 2017-01-25
      • 2023-03-28
      相关资源
      最近更新 更多