$sql = "select distinct id from tb order by id";
$result = mysql_query($sql) or die(mysql_error());
while($data = mysql_fetch_row($result)){
echo "{$data[0]} - ";
$sql="select data from tb where id='{$data[0]}' ";
$result2 = mysql_query($sql) or die(mysql_error());
while($data2 = mysql_fetch_row($result2)){
echo "{$data2[0]}, ";
}
echo "<br />\n";
}
$result = mysql_query("
SELECT
CONCAT(`id`, ' ', GROUP_CONCAT(DISTINCT `data` ORDER BY `data` SEPARATOR ' , ')) AS `output`
FROM `ชื่อตารางของคุณ`
GROUP BY `id`
ORDER BY `id`
");
while (($row = mysql_fetch_assoc($result))) {
echo "$row[output]<br />";
}
$sql = "SELECT id, concat_ws( ',', group_concat( distinct data order by data ) ) FROM `b` group by id ";
$result = mysql_query($sql) or die(mysql_error());
while($data = mysql_fetch_row($result)){
echo "{$data[0]} - {$data[1]} <br />\n";
}