2013-02-05 6 views
-1

나는 스레드 테이블과 코멘트 테이블이 있습니다. 스레드는 많은 의견을 가지고 있습니다 mysql 질의를 할 때. 많은 의견 때문에 중복 스레드가됩니다. tid를 그룹화하는 방법?PHP 루프 반복 foreach 중복

나는 foreach를 PHP에서 시도했지만 여전히 생각이 없습니다.

Array 
(
    [0] => Array 
     (
      [tid] => 110 
      [title] => This is question 
      [vid] => 175 
      [yesno] => 1 
     ) 

    [1] => Array 
     (
      [tid] => 110 
      [title] => This is question 
      [vid] => 179 
      [yesno] => 0 
     ) 
) 

내가 실제로

Array 
(
    [0] => Array 
     (
      [tid] => 110 
      [title] => This is question 

      [0] => Array(
         [vid] => 175 
         [yesno] => 1 
        ) 
      [1] => Array(
         [vid] => 176 
         [yesno] => 0 
        ) 
     ) 

) 
+1

사용과 같은 쿼리 뭔가를 사용할 수 있습니다 원하는', 별개의 thread.tid, thread.title을 선택 ... ' –

+1

'GROUP BY'를 입력하십시오. – BenM

+1

'group by thread.tid'도 작동합니다. –

답변

0

당신이

"SELECT ... FROM tablename GROUP BY thread.tid" 
     ^--- Field name 
0
$query='select distinct tid,title from thread order by tid'; 
$thread_id=mysql_query($query); 
while($result=mysql_fetch_assoc($thread_id)) 
{ 
    $data[]=$result; 
    $query1='select vid,yesno from comments where tid='.$result['tid']; 
    $comment=mysql_query($query1); 
    while($result1=mysql_fetch_assoc($comment)) 
    { 
     $data['comments'][]=$result1; 
    } 
} 
print_r($data); 
+0

1 쿼리를 사용하는 것은 어떻습니까? LEFT JOIN을 사용하기 때문에 – vzhen