CodeIgniter Forums
Active Record - Group By - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Active Record - Group By (/showthread.php?tid=59459)



Active Record - Group By - El Forum - 10-08-2013

[eluser]Unknown[/eluser]
The tried code:

Code:
$this->db->select('*');
        $this->db->from('documents_table');
        $this->db->order_by("last_modified_date", "desc");
        $this->db->group_by('document_id');

        $query = $this->db->get();

Table structure:

id , document_id , content , last_modified_date


Problem:

The design of the system means that when documents are updated, a actual new record is created with the same document_id and the updated content.

Now I need the documents displayed in a list, however if they are multiple documents with the same document_id (basically older versions of the file), only the latest version should be displayed. The current code outputs the first versions only, so this needs to be switched.

Is group_by the right method for this? Anyone with some experience can work out why its not working as intended, or am I just doing it wrong?