[英]Codeigniter - where primary key is used by child table
I have two tables blog
and category
, where blog.categoryid=category.id
. 我有两个表
blog
和category
,其中blog.categoryid=category.id
。 In the below snippet I am obtaining list of active categories with atleast one blog post. 在下面的代码段中,我将获得至少一篇博客文章中的活动类别列表。
$this->db->select('c.*',FALSE);
$this->db->from('category c');
$this->db->where('c.cattype','posts');
$this->db->where('c.activefrom <=', date('Y-m-d'));
/* TODO - where category has atleast one blog post */
$this->db->limit(10,$offset);
$query = $this->db->get();
$result = $query->result_array();
The above snippet provides the active category list, but I need to achieve list of active categories with atleast one blog post 上面的代码片段提供了活动类别列表,但是我需要通过至少一篇博客文章来获得活动类别列表
If you join (inner join) category
table to blog
table and group by category.id
, then you have a list of categories with at least one blog post. 如果将
category
表加入(内部加入) blog
表并按category.id
分组,那么您将拥有一个类别列表,其中至少包含一个博客文章。
Add these lines: 添加这些行:
$this->db->join('blog', 'blog.categoryid = category.id');
$this->db->group_by('c.id');
More information at: https://www.codeigniter.com/userguide2/database/active_record.html 有关更多信息, 请访问: https : //www.codeigniter.com/userguide2/database/active_record.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.