我有2个桌子forum_category和forum_topics。问题是, 最新的帖子不准确......我不知道它的查询。
任何人都可以帮助我吗?
示例输出在图像上......我使用codeigniter来表示
category
包含列id,name,desc topic
包含id,subject,desc,date_posted和posted_by 查询:
$select = array(
'forum_category.f_cat_name',
'forum_category.f_cat_id',
'forum_category.f_cat_desc',
'forum_topic.topic_subject as last_posted',
'max(forum_topic.date_posted) as last_date_posted',
'max(forum_topic.posted_by) as last_posted_by',
'count(forum_topic.topic_id) as total'
);
$query=$this->db
->select($select)
->from('forum_category')
->where('f_cat_type',1)
->where('f_cat_status',1)
->join('forum_topic','forum_topic.f_cat_id = forum_category.f_cat_id','inner')
->group_by('forum_category.f_cat_id')
->get();
return $query->result();
答案 0 :(得分:0)
Join子句应该出现在where子句之前。
试试这个,
$query=$this->db
->select($select)
->from('forum_category')
->join('forum_topic','forum_topic.f_cat_id = forum_category.f_cat_id','inner')
->where('f_cat_type',1)
->where('f_cat_status',1)
->group_by('forum_category.f_cat_id')
->get();
return $query->result();