2 つのテーブルを結合して結果を返すモデル コードを作成しました。
以下のテーブル構造から、私のモデル コードは最後の質問をスキップして 2 つの質問数のみを表示しています。少し調査した結果、3 番目の質問がカウントされない理由がわかりました。それは、answerテーブルに回答がないためです。
答えがない場合は、特定の質問に対して count=0 を表示する必要があります。この問題を解決するにはどうすればよいですか?
テーブル構造
question
-----------
question_id PK Auto_Incr  
question    varchar... 
votes       int
answer
------------
answer_id    PK  Auto_icre
question_id  FK refrences question  
content      longtext
テーブル データ構造データ:
 question
-----------
 question_id    question          votes
    1           what's name?       0
    2           where you?         3
    3           blah blah          9 
answer 
----------
 answer_id      question_id        content
    4              2                 India
    5              2                 Nepal
    6              2                 Pakistan
    7              1                 Mr Osama Binladan
モデル
public function fetch_allquestions($limit, $start) 
{
    $this->load->database(); 
    $this->db->limit($limit, $start);   
    $this->db->from('question');
    $select =array(
                   'question.*',
                   'userdetails.*',
                   'COUNT(answer.answer_id) AS `Answers`'
                  );
    $this->db->select($select);
    $this->db->join('answer','answer.question_id = question.question_id'); 
    $this->db->join('userdetails','userdetails.user_id = question.user_id'); 
    $query = $this->db->get();
    print_r("Number of rows=".$query->num_rows());//showing only One, out of 26 rows
    if ($query->num_rows() > 0)
    {
        foreach ($query->result() as $row)
        {
            $data[] = $row;
        }
        return $data;
    }
    else
    {
        return false;
    }
}