[thelist] sql group one field
Burhan Khalid
thelist at meidomus.com
Wed Sep 1 01:31:46 CDT 2004
On Tue, 2004-08-31 at 23:18, Richard Bennett wrote:
> I am trying to extract a list from a mySQL database :
>
> I have this table:
>
> serie: model:
> 1234 abc
> 1233 abc
> 1222 abc
> 1111 def
> 1112 def
>
> I would like this result:
>
> 1234,1233,1222 abc
> 1111,1112 def
>
> Any ideas?
I'm sure there is a one-shot way of doing this, but here would be my
attempt (PHP example to show algorithm -- without error checking):
$results = mysql_query("SELECT UNIQUE model FROM `tablename`");
$output = array();
while($row = mysql_fetch_assoc($results))
{
$query = "SELECT serie FROM `tablename` WHERE model =
'".$row['model']."'";
$r2 = mysql_query($query);
while($row2 = mysql_fetch_assoc($r2))
{
$temp[] = $row2['serie'];
}
$output[$row['model']] = implode(",",$temp);
$temp = array();
}
This would give you:
echo $output['abc'];
1234,1233,1222
More information about the thelist
mailing list