Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

GROUP_CONCAT

Posted on 2010-11-08
2
Medium Priority
?
772 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 2000 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

Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

Question has a verified solution.

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

Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
This post looks at MongoDB and MySQL, and covers high-level MongoDB strengths, weaknesses, features, and uses from the perspective of an SQL user.
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

688 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