-1

我有一个线程表和一个评论表。当我做mysql查询时,一个线程可以有很多评论。由于许多评论,它成为重复线程。如何分组 tid?

我在php中尝试了foreach,但仍然不知道。

   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
                   )
        )

)
4

2 回答 2

0

您可以使用类似这样的查询

"SELECT ... FROM tablename GROUP BY thread.tid"
         ^ --- Field name
于 2013-02-05T09:41:46.303 回答
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);
于 2013-02-05T10:04:52.287 回答