2011-08-22 9 views
0

私は共通のIDを共有する2つのテーブルを結合しようとしており、1つの行を複数回返し続けます。これは、モデルから機能ですCodeIgniterのクエリが同じ行を何度も返すのはなぜですか?

で働いています:

function get_latest_pheeds() { 
    $data = $this->user_keywords($this->ion_auth->user_id); 
    $keyword = $data; 
    $user_id = $this->ion_auth->user_id; 
    foreach($keyword as $key => $word) { 
     $q = "SELECT *,COUNT(pheed_comments.comment_id) as comments 
       FROM pheeds 
       LEFT JOIN pheed_comments ON pheed_comments.P_id=pheeds.pheed_id 
       WHERE pheed LIKE '%$word%' OR user_id='$user_id' 
       ORDER BY datetime DESC"; 
     $result = $this->db->query($q); 
     $rows[] = $result->result(); 
    } 
    return $rows; 
} 

は、私のクエリ間違ってますか?

答えて

5

あなたはSELECT *を使用しているので、COUNT()で返さ多くの列があるかもしれない、あなたのCOUNT()

  $q = "SELECT *,COUNT(pheed_comments.comment_id) as comments 
      FROM pheeds 
      LEFT JOIN pheed_comments ON pheed_comments.P_id=pheeds.pheed_id 
      WHERE pheed LIKE '%$word%' OR user_id='$user_id' 
      GROUP BY pheed_id, pheed 
      ORDER BY datetime DESC"; 

ためGROUP BYが欠落しています。正確な結果を得るには、GROUP BY

GROUP BY pheed_id, pheed, othercol1, othercol2, othercol3 
関連する問題