簡體   English   中英

Codeigniter-子表使用主鍵

[英]Codeigniter - where primary key is used by child table

我有兩個表blogcategory ,其中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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM