Solved

GROUP_CONCAT

Posted on 2010-11-08
2
769 Views
Last Modified: 2012-05-10
I'm trying to return all rows from a Business table (where RegionID is 1) while joining all phone numbers for that Business from the BusinessPhoneNumber table into a concatenated string, in a new column (say PhoneNumbers). My understanding is this can be done with GROUP_CONCAT() - here is my current SQL:

SELECT * FROM `Business`
LEFT JOIN `BusinessPhoneNumber` ON `BusinessPhoneNumber`.`ParentID` = `Business`.`ID`
WHERE `Business`.`RegionID` = 1

Please provide an example that uses GROUP_CONCAT to place the BusinessPhoneNumber's in a comma separated columns. FYI - the phone number field in that table is simply Phone (BusinessPhoneNumber.Phone).
0
Comment
Question by:level9wizard
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
2 Comments
 
LVL 58

Expert Comment

by:cyberkiwi
ID: 34087343
SELECT B.*, P.PhoneList
FROM Business B
LEFT JOIN
(
SELECT Business.ID, group_concat(BusinessPhoneNumber.Phone) PhoneList
FROM Business
INNER JOIN BusinessPhoneNumber ON BusinessPhoneNumber.ParentID = Business.ID
WHERE Business.RegionID = 1
) P ON P.ID = B.ID
WHERE B.RegionID = 1
0
 
LVL 58

Accepted Solution

by:
cyberkiwi earned 500 total points
ID: 34087349
Sorry left out a group by

SELECT B.*, P.PhoneList
FROM Business B
LEFT JOIN
(
SELECT Business.ID, group_concat(BusinessPhoneNumber.Phone) PhoneList
FROM Business
INNER JOIN BusinessPhoneNumber ON BusinessPhoneNumber.ParentID = Business.ID
WHERE Business.RegionID = 1
GROUP BY Business.ID
) P ON P.ID = B.ID
WHERE B.RegionID = 1
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Foreword This is an old article.  Instead of using the MySQL extension that was used in the original code examples, please choose one of the currently supported database extensions instead.  More information is available here: MySQLi / PDO (http://…
Creating and Managing Databases with phpMyAdmin in cPanel.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question