[英]Codeigniter MySQL JOIN Query
Im a beginner in MySQL, I got a hard time joining 2 tables in Codeigniter. 我是MySQL的初学者,我很难在Codeigniter中加入2个表。 So let me explain my tables.
因此,让我解释一下我的表。
In the first table named forum_topic I got these columns 在名为forum_topic的第一个表中,我得到了这些列
topic_id topic_sk topic_subject topic_desc posted_by date_posted topic_id topic_sk topic_subject topic_desc posted_by date_posted
1 123Qwe Rules and Regulations Anything Jonas Dulay 2015-11-03 11:15 PM 1123Qwe规章制度Jonas Dulay 2015-11-03 11:15 PM
The second table is named forum_comment 第二个表命名为forum_comment
comment_id topic_sk comment_id topic_sk
1 123Qwe 1123Qwe
2 123Qwe 2123Qwe
The expected result will be like this 预期结果将是这样
Topics Replies Date Posted by 主题回复日期发表者
Rules and Regulation 2 2015-11-03 11:15 PM Jonas Dulay 规章制度2 2015-11-03 11:15 PM Jonas Dulay
I dont know the process of getting the count of number of comment in other table 我不知道在其他表格中获取评论数的过程
then join it . 然后加入。 This is my query but it seems it will not help you.
这是我的查询,但似乎无济于事。
$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();
Please try below query, I think this is useful for you: 请尝试以下查询,我认为这对您有用:
$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();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.