Unix Technical Forum

Group by and concatenate

This is a discussion on Group by and concatenate within the MySQL General forum forums, part of the MySQL category; --> Hi, I have the following data: mysql> select Dealername,pc from ford_gb where pc='LE4 7SL'; +-----------------------------+---------+ | Dealername | pc ...


Go Back   Unix Technical Forum > Database Server Software > MySQL > MySQL General forum

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 02-27-2008, 09:31 PM
Andrew Braithwaite
 
Posts: n/a
Default Group by and concatenate

Hi,

I have the following data:

mysql> select Dealername,pc from ford_gb where pc='LE4 7SL';

+-----------------------------+---------+
| Dealername | pc |
+-----------------------------+---------+
| CD Bramall Ford - Leicester | LE4 7SL |
| CD Bramall Ford - Leicester | LE4 7SL |
| CD Bramall Ford - Leicester | LE4 7SL |
+-----------------------------+---------+

Is there a way in a single SQL query to group by Dealername, and have
the postcodes concatenated into a comma-separated list? e.g.

+-----------------------------+----------------------------+
| Dealername | concat_pc |
+-----------------------------+----------------------------+
| CD Bramall Ford - Leicester | LE4 7SL, LE4 7SL, LE4 7SL |
+-----------------------------+----------------------------+

Running MySQL 4.1 on Fedora C3.

Any help appreciated,

Andrew
SQL, Query
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 02-27-2008, 09:31 PM
Dan Buettner
 
Posts: n/a
Default Re: Group by and concatenate

Andrew, very possible, have a look at the GROUP_CONCAT function:
http://dev.mysql.com/doc/refman/5.0/...functions.html

Dan


On 10/11/06, Andrew Braithwaite <Andrew@multimap.com> wrote:
> Hi,
>
> I have the following data:
>
> mysql> select Dealername,pc from ford_gb where pc='LE4 7SL';
>
> +-----------------------------+---------+
> | Dealername | pc |
> +-----------------------------+---------+
> | CD Bramall Ford - Leicester | LE4 7SL |
> | CD Bramall Ford - Leicester | LE4 7SL |
> | CD Bramall Ford - Leicester | LE4 7SL |
> +-----------------------------+---------+
>
> Is there a way in a single SQL query to group by Dealername, and have
> the postcodes concatenated into a comma-separated list? e.g.
>
> +-----------------------------+----------------------------+
> | Dealername | concat_pc |
> +-----------------------------+----------------------------+
> | CD Bramall Ford - Leicester | LE4 7SL, LE4 7SL, LE4 7SL |
> +-----------------------------+----------------------------+
>
> Running MySQL 4.1 on Fedora C3.
>
> Any help appreciated,
>
> Andrew
> SQL, Query

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 02:31 AM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com