我是MySQL的初学者,我很难在Codeigniter中加入2个表。所以让我解释一下我的表格。
在第一个名为forum_topic的表中,我得到了这些列
topic_id topic_sk topic_subject topic_desc posted_by date_posted
1 123Qwe规则和条例任何事情Jonas Dulay 2015-11-03 11:15 PM
第二个表名为forum_comment
comment_id topic_sk
1 123Qwe
2 123Qwe
预期的结果将是这样的
主题回复日期由。发布
规则和条例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();
答案 0 :(得分:1)
请尝试以下查询,我认为这对您有用:
$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();