Solved

GROUP_CONCAT

Posted on 2010-11-08
2
764 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
  • 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

A lot of articles have been written on splitting mysqldump and grabbing the required tables. A long while back, when Shlomi (http://code.openark.org/blog/mysql/on-restoring-a-single-table-from-mysqldump) had suggested a “sed” way, I actually shell …
Both Easy and Powerful How easy is PHP? http://lmgtfy.com?q=how+easy+is+php (http://lmgtfy.com?q=how+easy+is+php)  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

743 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now