0

Is there a way or can someone translate this to code igniter query builder.

SELECT result.id, result.name, result.added
FROM (SELECT tbl.id, tbl.name, tbl.date_added
      FROM table tbl
      GROUP BY tbl.id) result
GROUP BY result.date_added

I already have my research(link below) but can't find anything like this(query above). https://www.codeigniter.com/userguide3/database/query_builder.html

https://arjunphp.com/how-to-write-subqueries-in-codeigniter-active-record/

And yes, this can be done using Stored Procedure but I have another reason why I need to implement this as query builder.

1
  • That's a funky operation you have there. What are you actually trying to achieve? If you can explain in plain English and provide some sample data and your expected result, we'll be able to provide better guidance. Commented Oct 22 at 3:06

1 Answer 1

1

try this one.

// Sub Query
         $this->db->select('result.id, result.name, result.added')->from('table tbl');
     $subQuery =  $this->db->get_compiled_select();

// Main Query
     $this->db->select('result.id, result.name, result.added')
     ->from('table tbl')
     ->where("id IN ($subQuery)", NULL, FALSE)
     ->get()
     ->result();
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.