Mysql
 sql >> Base de Dados >  >> RDS >> Mysql

MySQL (ou PHP?) agrupa os resultados por dados de campo


Esta seria a minha solução, embora não seja nada elegante
<?php
$dbc = new MySQLI(DBHOST,DBUSER,DBPASS,DB);
$result = $dbc->query("
SELECT
p.Group as 'group',
GROUP_CONCAT(name) as names
FROM prueba p
GROUP BY p.Group
");
?>
<table>
<tr>
    <th>Group</th>
    <th>Name</th>
</tr>
<?php while($row = $result->fetch_assoc()){
    $names = split(",",$row["names"]);
?>
    <tr>
        <td><?php echo $row["group"] ?> </td>
        <td><?php echo $names[0]; array_shift($names) ?></td>
    </tr>
    <?php foreach( $names as $name){ ?>
        <tr>
            <td></td>
            <td><?php echo $name ?></td>
        </tr>
    <?php } ?>
<?php } ?>
</table>