mysqlcodeigniteractiverecorduniquedistinct

How to fetch unique rows from a database table with CodeIgniter?


I am running a query to populate a dropdown menu.

My trouble is that the column I am using contains duplicate company names. Is there a way adjust my CodeIgniter query to only get unique company names in my result set?

For instance, if I have the following company names in the table:

Company 1
Company 1
Company 2
Company 3
Company 4
Company 4

I would like to pass only unique company names to the dropdown field.

Company 1
Company 2
Company 3
Company 4

I am using the below active record script to generate the SQL. I need to know what I should use to only show each unique company name once.

function getAllUsers() {
    $this->db->select('*');
    $this->db->from('userProfileTable');
    
    $query = $this->db->get();
    return $query->result_array();
}

Or more simply, what would the raw SQL be? Then I can work on translating that into query builder methods.


Solution

  • Use:

    $this->db->distinct();
    $this->db->select('*');
    $this->db->from('userProfileTable');
    $query = $this->db->get();
    return $query->result_array();