Udders
Udders

Reputation: 6976

mySQL help in codeigniter

I am running a query to populate a dropdown menu, however the column I am using a list of company names, this means that the company name is often repeated, is there way to only get each repeated value only once? So for instance if I have in the table something like,

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

But I would like the dropdown to return,

Company 1
Company 2
Company 3
Company 4

I am using the database libray and active record to write the sql like this currently I need to know what I use to only show each result once,

function getAllUsers() {
    $this->db->select('*');
    $this->db->from('userProfileTable');

    $query = $this->db->get();
    return $query->result_array();
}

Or what would the raw sql be?

Upvotes: 0

Views: 706

Answers (4)

Pedro
Pedro

Reputation: 2927

Use:

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

Upvotes: 3

Randell
Randell

Reputation: 6170

You can use $this->db->distinct(); which adds the DISTINCT keyword to your query.

Modify your function to this:

function getAllUsers() {
    $this->db->distinct();
    $query = $this->db->get('userProfileTable');

    return $query->result_array()
}

which produces

SELECT DISTINCT * FROM userProfileTable

Please note that select and from have been removed from your original function because get simply does the same thing.

Upvotes: 1

Industrial
Industrial

Reputation: 42768

This should do the trick:

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

Upvotes: 0

noonex
noonex

Reputation: 2075

Use DISTINCT keyword:

SELECT DISTINCT * from userProfileTable;

Upvotes: 0

Related Questions