0

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.

5 Answers 5

3

Use:

$this->db->distinct();
$this->db->select('*');
$this->db->from('userProfileTable');
$query = $this->db->get();
return $query->result_array();
Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

0

Use DISTINCT keyword:

SELECT DISTINCT * from userProfileTable;

1 Comment

have you read this part of question: "Or what would the raw sql be?" ?
0

This should do the trick:

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

Comments

0

You did not share any sample database table data, so we don't 100% know if your rows with shared company names might have unique values in other columns. This is important because if other columns have unique values, then merely adding DISTINCT before * will not work as needed. If full rows are identical, then I'd say you have a flaw in your data storage strategy.

Using SELECT DISTINCT * FROM userProfileTable:

public function getUniqueUsers(): array
{
    return $this->db
        ->distinct()
        ->get('userProfileTable')
        ->result_array();
}

Using SELECT DISTINCT id, name FROM userProfileTable:

public function getUniqueUsers(): array
{
    return $this->db
        ->distinct()
        ->select('id, name')
        ->get('userProfileTable')
        ->result_array();
}

If using GROUP BY, nominate the grouping columns in the SELECT clause because * will cause trouble when aggregate functions are expected.
Using SELECT id, name FROM userProfileTable GROUP BY id, name:

public function getUniqueUsers(): array
{
    return $this->db
        ->select('id, name')
        ->group_by('id, name')
        ->get('userProfileTable')
        ->result_array();
}

Finally, there may be performance ramifications, so read this for further insights:
What's faster, SELECT DISTINCT or GROUP BY in MySQL?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.