CodeIgniter Forums
Sub Queries or better way to do this? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: Best Practices (https://forum.codeigniter.com/forumdisplay.php?fid=12)
+--- Thread: Sub Queries or better way to do this? (/showthread.php?tid=65234)



Sub Queries or better way to do this? - BamaStangGuy - 05-18-2016

I need to take this query:


PHP Code:
    public function get_active_pools() {
        $this->db->where('active''1');
        $query $this->db->get('pools');
        return $query->result();
    


and join it to a second table that is related by an id and then count how many times the id of the first table is listed in the second table. What is my best method of doing this with Query Builder?


RE: Sub Queries or better way to do this? - fakhrawy - 05-18-2016

first you need to add join line to be like this
$this->db->join('d_pools_second','d_pools_second.key_id=pools.pl_id');
then return by count like this
return $query->num_rows();