gpt4 book ai didi

php - Codeigniter Active Record 中的多个选择计数 SQL 查询

转载 作者:行者123 更新时间:2023-11-29 04:01:22 25 4
gpt4 key购买 nike

我喜欢将以下 MySQL 查询转换为 Codeigniter Active Record 查询。

以下是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

代码点火器:

$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;

请帮助我将上述代码转换为 Codeigniter Active Record。

最佳答案

这里是事件记录中的查询

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

关于php - Codeigniter Active Record 中的多个选择计数 SQL 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17250993/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com