这是我的查询。我想获取总数和记录总数。

 function getCountByType($inventory_type) {

        $this->db->select_sum('quantity','total_quantity');
        if($inventory_type)
        $this->db->where('inventory_type',$inventory_type);
        $query = $this->db->get($this->tableInventory);
        $resultdata = $query->result();
        $result['total_count'] = $query->num_rows();
        $result['total_quantity'] = $resultdata[0]->total_quantity;
        return $result;
    }


它给了我正确的数量总和,但num_rows每次都返回1。即使我有10记录,它也只给出1记录,而当我remove select_sum时,它将很好地工作。

最佳答案

Sum是聚合函数,最后将您的行分组为单个put sum查询。

function getCountByType($inventory_type) {
        if($inventory_type)
        $this->db->where('inventory_type',$inventory_type);
        $query = $this->db->get($this->tableInventory);
        $resultdata = $query->result();
        $result['total_count'] = $query->num_rows();
        $this->db->select_sum('quantity','total_quantity');
        $result['total_quantity'] = $resultdata[0]->total_quantity;
        return $result;
    }

关于php - select_sum和num_rows在codeigniter中无法一起使用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/30460172/

10-10 03:34