![](/img/trans.png)
[英]How to print a name of primary key which is used multiple times in other table in codeigniter
[英]Codeigniter - where primary key is used by child table
我有兩個表blog
和category
,其中blog.categoryid=category.id
。 在下面的代碼段中,我將獲得至少一篇博客文章中的活動類別列表。
$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();
上面的代碼片段提供了活動類別列表,但是我需要通過至少一篇博客文章來獲得活動類別列表
如果將category
表加入(內部加入) blog
表並按category.id
分組,那么您將擁有一個類別列表,其中至少包含一個博客文章。
添加這些行:
$this->db->join('blog', 'blog.categoryid = category.id');
$this->db->group_by('c.id');
有關更多信息, 請訪問: https : //www.codeigniter.com/userguide2/database/active_record.html
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.