开发者

codeigniter join show certain number of rows

Hello i want to know how i can show how many "comments" there is posted in a "topic" i have made this

    $this->db->select('forum_traad.id as traadID, 
    forum_traad.overskrift as traadOverskrift, forum_komment开发者_C百科are.brugernavn as kommentareBrugernavn, 
    forum_kommentare.dato as kommentareDato, forum_traad.status as traadStatus, 
    forum_kommentare.id as kommentareID');
    $this->db->join('forum_kommentare', 'forum_traad.id = forum_kommentare.fk_forum_traad');
    //$this->db->where('traadStatus', 'ja');
    $this->db->limit('10');
    $this->db->order_by('kommentareDato', 'desc');
    $this->db->from('forum_traad');

    $activeQuery = $this->db->get();

    return $activeQuery->result();

kommentare = comments

traad = threads


If you just need the count of comments per thread, and not the actual comments themselves:

$this->db->select('forum_traad.id as traadID, forum_traad.overskrift as traadOverskrift, 
    forum_traad.status as traadStatus, count(forum_kommentare.id) as kommentare_count');
$this->db->join('forum_kommentare', 'forum_traad.id = forum_kommentare.fk_forum_traad');
$this->db->group_by('forum_traad.id');
$this->db->from('forum_traad');

$activeQuery = $this->db->get();

return $activeQuery->result();

If you need the count and the data for the comments you'll need essentially two queries (which could be combined in one call using a subquery). You'd add the above example as a subquery to your original and join in on it.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜