How do i write in codeigniter in active record the following query?
select tbl_list.id, tbl_list_items.name
from tbl_list
join tbl_list_items
on tbl_list_items.id=(select tbl_list_items.id from tbl_list_items where list_id=tbl_list.id order by tbl_list_items.item_no asc LIMIT 1)
Update:
This is what i come out with but it does not do the job.
$this->db->select($this->table_list.'.*');
$this->db->from($this->table_list);
$sub = $this->subquery->start_subquery('join','',$this->table_list_items.'.id');
$sub->select('id')->from($this->table_list_items)->where('list_id','tbl_list.id')->order_by($this->table_list_items.'item_no','asc')->limit('1');
$this->subquery->end_subquery('list_items');
Appreciate any helps. Thanks.
Review the Active Record methods available to you in CodeIgniter.
Then review a CI library to extend it to handle subqueries available on CodeIgniter
There is a walk-through about how CI is doing this under-the-hood
Found a github codebase for implementing subqueries in CodeIgniter 1.7 and 2.x (see link)
EDIT:
Revised Code is below to assist. You can also check out the 4th link’s examples.
To replicate what you’er trying to do, the code would be similar to (this is un-tested… but hopefully starts you on the right path):
The key is this statement: $this->subquery->end_subquery(‘tli’);
What happens there, is that it treats the result of the subquery as AS tli so that you can reference it in tli.id.
Hope this helped!