Welcome Guest, Not a member yet? Register   Sign In
Active Record - Group By
#1

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




Theme © iAndrew 2016 - Forum software by © MyBB