web-dev-qa-db-fra.com

Comment utiliser 'DISTINCT' dans Codeigniter Active Records?

J'ai une requête utilisant des enregistrements actifs.

$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);
$query = $this->db->get('users_gcm_registration as reg');

Je veux chercher DISTINCT(registration_id).

Comment puis je faire ça?

5
Saswat

Vous pouvez utiliser distinct comme

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

Mais mieux utiliser 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');
11
Saty

Il suffit d'utiliser $this->db->distinct(); C'est tout. Cela va distinguer la valeur ...

J'espère que cela fonctionne bien pour vous ...

3
user5139148
$this->db->select('reg.users_id,DISTINCT(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);
$query = $this->db->get('users_gcm_registration as reg');
1
Shibon
   $query = $this->db->select('registration_id')
                ->distinct('registration_id')
                ->from('reg')
                ->get(); 
 return $query->result();
1
Avigit.M