Sparkz
Sparkz

Reputation: 83

Multiple Select Count SQL queries in Codeigniter Active Record

I like to convert following MySQL queries into Codeigniter Active Record Queries.

Following is MySQL:

select brand_id,name,(select count(*) from items where brand_id = b.brand_id) as itemc, (select count(*) from models where brand_id = b.brand_id) as modelc from brands as b

Codeigniter :

$this->db->limit($perpage,$page);
    $query = 'select brand_id,name,(select count(*) from items where brand_id = b.brand_id) as itemc, (select count(*) from models where brand_id = b.brand_id) as modelc from brands as b limit '.$perpage.' offset '.$page.'';
    $query = $this->db->query($query);
    $query = $query->result();
    return $query;

Kindly help me to get the above codes converted for Codeigniter Active Record.

Upvotes: 1

Views: 9662

Answers (1)

umefarooq
umefarooq

Reputation: 4574

here is the query in active records

 $rows =   $this->db->select('b.brand_id,b.name')
    ->select('(select count(*) from items where brand_id = b.brand_id) as itemc',FALSE)
    ->select('(select count(*) from models where brand_id = b.brand_id) as modelc',FALSE)
    ->from('brands as b')
    ->limit($perpage,$page)->get()->result();

Upvotes: 8

Related Questions