Welcome Guest, Not a member yet? Register   Sign In
[solved] Get $this->db->distinct() to work
#1

[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
#2

[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)
#3

[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.
#4

[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!
#5

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




Theme © iAndrew 2016 - Forum software by © MyBB