我想爲我的新聞報道頁面的嵌套查詢提供一些幫助 - 基本上我希望每篇文章都有相關的評論顯示在下面,但目前只對每篇文章返回一條評論:(從兩張桌子上拉取信息
function get_records($limit, $offset, $sort) {
$this->db->select('news.*, COUNT(comments.news_id) as comments, comments.comment as comment, news.id as id, news.created_on as created_on, CONCAT(users.firstname, " ", users.surname) as author, categories.category as category, news_types.type as news_type', FALSE);
$this->db->from('news', 'comments');
$this->db->join('users', 'users.id = news.author', 'left');
$this->db->join('comments', 'comments.news_id = news.id', 'left');
$this->db->join('categories', 'categories.id = news.category', 'left');
$this->db->join('news_types', 'news_types.id = news.news_type', 'left');
$this->db->group_by('news.id');
$this->db->order_by('news.id', 'DESC');
$this->db->limit($limit, $offset);
$query = $this->db->get();
if($query->num_rows() > 0) {
return $query->result_array();
}
}