Counting results filtered by distinct() or group_by() in Codeigniter Counting results filtered by distinct() or group_by() in Codeigniter codeigniter codeigniter

Counting results filtered by distinct() or group_by() in Codeigniter


    $this->db->select('COUNT(id)');    $this->db->join();    $this->db->distinct();    $this->db->group_by();//...etc ...    $query = $this->db->get('mytable');    return count($query->result()); 

or

    $this->db->join();    $this->db->distinct();    $this->db->group_by();//...etc ...    $query = $this->db->get('mytable');    return $query->num_rows(); 


You can use

$this->db->group_by();

otherwise

$this->db->distinct();


In my case, using Codeigniter 3.1.11 as of today, distinct() is taken into account when doing count_all_results(). HOWEVER you have to use the function, doing a $this->db->select("distinct x") and then the counting, won't apply the DISTINCT limitation for the record count.