CodeIgniter Forums

Full Version: [solved] Get $this->db->distinct() to work
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

[eluser]soamazing[/eluser]
Hi all,

I'm quite new to code igniter, but so far so good! I only now have a problem which I can't see to solve. Below i'm extracting some data in my model. It works fine, except for the distinct part...

Does anyone know where to place this distinct line, and how to distinct it on the collumn 'name'.

Code:
$this->db->select('name,id,image,category_id,pnumber');
    $this->db->distinct('name');
    $this->db->where('country_id',$countryid);
    $this->db->orderby('name','asc');
    $Q = $this->db->get('products');

At this moment this extraction works only I still get duplicates with the same name. (so the distinct part is not being done. Looks like it get skipped for some reason)

Many thanks in advance!

Rick

El Forum

[eluser]soamazing[/eluser]
Does no-one have any idea? Seems to me like a simple mistake in how to use the code... (I wish i knew what it is Wink)

El Forum

[eluser]xwero[/eluser]
First you could try to remove the parameter from the distinct method because it doesn't need one, as second option you can try using group_by('name') instead of distinct.

El Forum

[eluser]soamazing[/eluser]
Thnx Xwero! That's all I needed.

Removing the parameter of distinct didn't work, also it should only make sure it doesn't get duplicates of items with the same name, while there can be duplicates in 'image' and 'category_id'.

The other option to use group_by('name') is the solution to my problem. It works perfectly! Have seen this function in the 'user guide' but didn't knew I could use it for this particular problem.

For the onces stumbling upon this topic while searching for a solution for the distinct function, my working end code now looks like;

Code:
$this->db->select('id,name,image,category_id,pnumber');
        $this->db->group_by('name');
    $this->db->where('country_id',$countryid);
    $this->db->orderby('name','asc');
    $Q = $this->db->get('products');

Again thanks for this simple solution xwero!

El Forum

[eluser]xwero[/eluser]
I did a check on the web and it seems the distinct keyword disregards the where part of the statement but the mysql documentation says otherwise. I think it will be a mysql version related problem.