0

i am trying to fetch records that have unique email address ( there are repeating emails as well in the database)

below is my query but it prints all the records

$this->db->distinct('email'); 
$this->db->select('*');
$this->db->from('booking');  
$query =    $this->db->get(); 
$data['booking']=$query->result();

Please help me to fix the issue . Thanks

  • You are using distinct wrong. See this link https://stackoverflow.com/questions/656622/codeigniter-how-to-do-a-select-distinct-fieldname-mysql-query – Forbs Oct 03 '18 at 22:06
  • Possible duplicate of [CodeIgniter: How To Do a Select (Distinct Fieldname) MySQL Query](https://stackoverflow.com/questions/656622/codeigniter-how-to-do-a-select-distinct-fieldname-mysql-query) – Forbs Oct 03 '18 at 22:06

2 Answers2

0

You can achieve your results in two ways: distinct and group_by

For reference, please view here

$this->db->select('*');
$this->db->from('booking');
$this->db->distinct('email');
$query = $this->db->get();
return $query->result();

You can use group_by as alternatives way in your active record query.

$this->db->select('*');
$this->db->group_by('email');// add group_by
$query = $this->db->get('booking');
return $query->result();
Muhammad Usman
  • 1,403
  • 13
  • 24
0

do it like this...

  $this->db->select('*');
  $this->db->from('booking');
  $this->db->group_by('email');
  $query = $this->db->get(); 

  $data['booking']=$query->result(); 

it will echo all unique email addresses except those duplicated email.

curiosity
  • 834
  • 8
  • 20