Codeigniter MySQL JOIN Query


Codeigniter MySQL JOIN Query

我是MySQL的初学者,在加入Codeigniter中的两个表时遇到了困难。让我来解释一下我的桌子。

在第一个名为forum_topic的表中,我得到了以下列

主题id主题sk主题主题主题描述发布日期发布
1 123Qwe规则和规定任何事情Jonas Dulay 2015-11-03 11:15 PM

第二个表命名为forum_comment
comment_id主题sk
1 123Q我们
2 123Q我们

预期结果是这样的
主题答复日期发布者
规则和条例2 2015-11-03 11:15 PM Jonas Dulay


我不知道在其他表格中获得评论数量计数的过程
然后加入。这是我的疑问,但似乎对你没有帮助

    $this->db->select('*,count(forum_comment.topic_sk) as count');
    $this->db->from('forum_topic');
    $this->db->where(array('f_cat_id'=>$cat,'topic_status'=>1))
    ->join('forum_comment','forum_comment.topic_sk = forum_topic.topic_sk','left')
    ->group_by('forum_topic.topic_sk');
    $this->db->order_by('pinned',"DESC");
    $this->db->order_by("date_posted","DESC");
    $query= $this->db->get();
    return $query->result();

请尝试以下查询,我认为这对您有用:

$this->db->select('ft.*,', FALSE);
 $this->db->select('IFNULL(COUNT(fc.topic_sk),0) as count', FALSE);
 $this->db->from('forum_topic as ft');
 $this->db->join("forum_comment as fc", ' rc.topic_sk = ft.topic_sk','left');
 $this->db->group_by('ft.topic_sk');
 $this->db->order_by('pinned',"DESC");
 $this->db->order_by("date_posted","DESC");
 $query= $this->db->get();
 return $query->result();