使用Codeigniter加入TAble进行计数

时间:2018-12-08 01:41:39

标签: sql codeigniter join

这是我的数据库

branch               category_branch
---------------      ---------------
id_branch            id
category_id          public(1/0)
branch_name          category_name

这是我在Model上的简单功能

function count_branch_all()
{
    return $this->db
            ->count_all_results('branch');
}

我想在public = 1的表分支上计算数据

请帮助我...

1 个答案:

答案 0 :(得分:0)

您必须将分支表与cateroty_branch表联接在一起,条件是:

function count_branch_all()
{
    $this->db->select('*');
    ->from('branch');
    ->join('category_branch','branch.category_id=category_branch.id', 'left');
    ->where('public',1);
    return = $this->db->count_all_results();
}