How to use 'DISTINCT' in Codeigniter Active Records?

Just use $this->db->distinct(); That's it. It will distinct the value...

Hope it works fine for you...


You have two way to fullfil this

  1. In Codeigniter way
  2. In SQL way

Codeigniter way

You can use $this->db->distinct() in your active record query.

$this->db->distinct();
$this->db->get('table_name');
...

In SQL way

$query = $this->db->query("SELECT DISTINCT * FROM table_name ...");
$result = $query->result_array();
return $result;

You can use distinct as

$this->db->distinct('reg.registration_id');
$this->db->select('reg.users_id,reg.device_type');
....

But better use group_by

$this->db->select('reg.users_id,reg.registration_id,reg.device_type');
$this->db->join('users as usr','usr.users_id = reg.users_id','left');
$this->db->where('usr.users_status',1);
$this->db->where('reg.users_id',91);
$this->db->group_by('reg.registration_id');// add group_by
$query = $this->db->get('users_gcm_registration as reg');