EllisLab text mark
Advanced Search
     
[solved] Get $this->db->distinct() to work
Posted: 14 October 2008 06:56 AM
Joined: 2008-10-14
4 posts

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’.

$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

 
Posted: 14 October 2008 10:20 AM   [ # 1 ]   [ Rating: 0 ]
Joined: 2008-10-14
4 posts

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)

 
Posted: 14 October 2008 10:27 AM   [ # 2 ]   [ Rating: 0 ]
Joined: 2006-07-14
4237 posts

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.

 
Posted: 14 October 2008 10:40 AM   [ # 3 ]   [ Rating: 0 ]
Joined: 2008-10-14
4 posts

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;

$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!

 
Posted: 14 October 2008 10:48 AM   [ # 4 ]   [ Rating: 0 ]
Joined: 2006-07-14
4237 posts

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.