问题
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:
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();
来源:https://stackoverflow.com/questions/51960117/codeigniter-subquery-in-query-builder