Well, there are several ways to do this. One way is hacking.
How can I rewrite this SQL code in Active Records CodeIgniter?
This other way is very simple.
$this->db ->select('ID') ->from('table2') ->order_by('id') ->limit('5'); $subquery = $this->db->_compile_select(); $this->db->_reset_select(); $query = $this->db ->select('t1.name') ->from('table1 t1 ') ->join("($subquery) t2","t2.id = t1.t2_id") ->get('table1 t1');
A few words about this.
You must use the sentence in the subqueries because get starts the request.
In codeigniter 2, _compile_select and _reset_select cannot be accessed because they are protected by methods.
You may need to remove the keyword before both methods in the system / database / DB _active_rec.php
This article is also helpful.
source share