![]() |
using $this->db->group_by() and $this->db->order_by() together - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20) +--- Forum: Archived General Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=21) +--- Thread: using $this->db->group_by() and $this->db->order_by() together (/showthread.php?tid=32145) |
using $this->db->group_by() and $this->db->order_by() together - El Forum - 07-14-2010 [eluser]chefnelone[/eluser] Hello, I can't make to use $this->db->group_by() and $this->db->order_by() together something like: Code: $this->db->group_by('id'); I need it to groupep but also ordered by 'order'. What can I do? using $this->db->group_by() and $this->db->order_by() together - El Forum - 07-14-2010 [eluser]mddd[/eluser] Why not? You can use GROUP BY and ORDER BY together just fine. What exactly is it that doesn't work? using $this->db->group_by() and $this->db->order_by() together - El Forum - 07-14-2010 [eluser]danmontgomery[/eluser] You're probably getting a "column XXX is ambiguous" error (are you joining more than one table with an "id" or "order" field?), and need to specify the table. using $this->db->group_by() and $this->db->order_by() together - El Forum - 07-14-2010 [eluser]chefnelone[/eluser] I have a table like: id -- group -- colour -- order 1 -- 1 -- black -- 0 2 -- 2 -- blue -- 2 3 -- 2 -- yellow -- 1 then I run the query: Code: $this->db->group_by('group'); If I use $this->firephp->log( $data ); I get this array: Code: array( But I need it to be (because order of 'yellow' is lower than 'blue') Code: array( using $this->db->group_by() and $this->db->order_by() together - El Forum - 07-14-2010 [eluser]danmontgomery[/eluser] When the query is executed "group by" is executed before "order by", so you're not going to get the result you expect with a simple query like that. using $this->db->group_by() and $this->db->order_by() together - El Forum - 07-14-2010 [eluser]chefnelone[/eluser] How should I do this then? using $this->db->group_by() and $this->db->order_by() together - El Forum - 07-14-2010 [eluser]mddd[/eluser] When using GROUP BY, you order the results that you get after GROUP BY. There is no way to influence which row you get inside each group; it is simply the first row (or if you have a WHERE clause, the first row to match the WHERE conditions). However, you could do the sorting first in a subquery. And then group the results in the main query. Code: SELECT * FROM (SELECT * FROM table ORDER BY order) AS my_subquery GROUP BY group ORDER BY group using $this->db->group_by() and $this->db->order_by() together - El Forum - 07-14-2010 [eluser]WanWizard[/eluser] If you group on a certain field, there will be one record in the result set for every value of that field found. The value of all other fields are random, and can not be predicted. Ordering is something that happens on the resultset, not before. So by the time you order on 'colour', grouping has already happened, and the colour and order field in the resultset are random (could be yellow, could be blue). using $this->db->group_by() and $this->db->order_by() together - El Forum - 07-14-2010 [eluser]chefnelone[/eluser] [quote author="mddd" date="1279134614"]When using GROUP BY, you order the results that you get after GROUP BY. There is no way to influence which row you get inside each group; it is simply the first row (or if you have a WHERE clause, the first row to match the WHERE conditions). However, you could do the sorting first in a subquery. And then group the results in the main query. Code: SELECT * FROM (SELECT * FROM table ORDER BY order) AS my_subquery GROUP BY group ORDER BY group it's clear. thanks |