두 개의 테이블을 결합하고 결과를 반환하는 모델 코드를 작성했습니다.정확한 행 수를 계산할 수 없습니다
아래 테이블 구조에서 내 모델 코드는 두 번째 질문을 건너 뛰고 있습니다. 약간의 연구 끝에 제 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;
}
}
@JoachimIsaksson 예, Thankyou.Working의 벌금을 조인을 사용합니다. 난 몰랐어. 이 조인에서 왼쪽으로 무엇을 의미합니까? litle 설명과 함께 답변으로 게시하십시오. 그래서 내가 당신의 대답을 받아 들일 수 있도록 –
@ BurlsWillis MySQL'LEFT JOIN', [그것에 관한 문서] 준비하기 (http://dev.mysql.com/doc/refman/5.5/en/left-join-optimization.html) . – Uby
@BurlsWillis 물론 답을 추가했습니다. –