codeigniter连接计数另一个表

时间:2016-04-09 14:32:42

标签: php mysql codeigniter join count

     $query=$this->db->select('*')
                     ->from('activation')
                     ->join('products','products.id_pro = activation.id_pro')
                     ->join('user','user.id = products.user_p')
                     ->order_by('id_key','DESC')
                     ->get();
     return $query->result();  

我有这些代码,我加入3个表abd的结果,效果很好。我需要的更多是来自4.table的行数。该表称为许可证,我需要计算id_key(表激活的主键)的行数。如何在我的代码中添加它?

2 个答案:

答案 0 :(得分:0)

尝试选择许可证列作为计数

     $query=$this->db->select('*, COUNT(license.id_key ) as license_count')
                 ->from('activation')
                 ->join('products','products.id_pro = activation.id_pro')
                 ->join('user','user.id = products.user_p')
                 ->join('license','license.id_key = table.column?')
                 ->order_by('id_key','DESC')
                 ->get();

访问对象中的计数,例如:$query->result()->license_count;

答案 1 :(得分:0)

     $query=$this->db->select('BaseTbl.*', 'Products.*', 'User.*', COUNT(License.id_key) as license_count)
                 ->from('activation as BaseTbl')
                 ->join('products as Products','Products.id_pro = BaseTbl.id_pro')
                 ->join('user as User','User.id = Products.user_p')
                 ->join('license as License','License.id_key = BaseTbl.id_key')
                 ->order_by('BaseTbl.id_key','DESC')
                 ->group_by('BaseTbl.id_key')
                 ->get();