php输出foreach中的重复值

时间:2016-03-01 09:30:39

标签: php mysql

我有一个像

这样的数据库(mysql)表
id |  assign_id  |  rights    | party
---+-------------+------------+---------
1  |5            |  12        | xyz
2  |5            |  3         | xyz
3  |6            |  14        | abc
4  |6            |  5         | abc

现在我想使用php foreach

以这种方式以表格格式输出
   |  assign_id  |  rights    | party
---+-------------+------------+---------
   |5            |  12,3      | xyz

   |6            |  5,14      | abc

表结构是                                                          

                      <th> Assign ID.     </th>
                      <th> Party Name     </th>
                      <th> rights</th>
                    </tr>

                  </thead>
                  <tbody>

                   <?php if(is_array($aryAssign)) { 
                         $cntuser=1;
                         foreach($aryAssign as $aryAssign){ ?>

                    <tr>

                      <td> <?php echo $aryAssign['assign_id']; ?>  </td>
                      <td> <?php echo $aryAssign['party']; ?>  </td>
                     <td> <?php echo $aryAssign['rights']; ?>    </td>
                                              </tr>
                    <?php   } }  ?> 

                  </tbody>
               </table>

1 个答案:

答案 0 :(得分:1)

为什么不使用GROUP_CONCAT

SELECT assign_id, party, GROUP_CONCAT(rights)
FROM mytable
GROUP BY assign_id, party