简体   繁体   English

Codeigniter活动记录中的多个Select Count SQL查询

[英]Multiple Select Count SQL queries in Codeigniter Active Record

I like to convert following MySQL queries into Codeigniter Active Record Queries. 我喜欢将以下MySQL查询转换为Codeigniter Active Record查询。

Following is MySQL: 以下是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 : 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. 请帮助我将上面的代码转换为Codeigniter Active Record。

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();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM