Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Concatenating results for equal conditions 1

Status
Not open for further replies.

maharg

Technical User
Mar 21, 2002
184
Hi folks,

I'm struggling to formulate a solution to this simple little task...


PHP:
foreach($db->query('SELECT zip, name FROM memberrecords ORDER BY zip ASC')as $record)
{
$zip=$record['zip'];
$name=$record['name'];
echo $zip."=".$name."<br>".PHP_EOL;
}

If I have 2 or more zip codes the same, I'd like to concatenate the names.
So, instead of outputting ...

102=Al Capone
123=Fred Bloggs
123=John Doe
123=Fred Fuzz
234=Henry Kissinger

I want to get ...

102=Al Capone
123=Fred Bloggs, John Doe, Fred Fuzz
234=Henry Kissinger

Any clues much appreciated!

Regards

Graham
 
sure
Code:
SELECT     zip, GROUP_CONCAT( name ) AS `name`
FROM       test
GROUP BY   zip ASC
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top