Solved

GROUP_CONCAT

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

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
count download link and run update query 9 70
mysql ide 10 40
AWS EC2 & RDS Instance 5 35
mysql date time 14 30
Fore-Foreword Today (2016) Maxmind has a new approach to the distribution of its data sets.  This article may be obsolete.  Instead of using the examples here, have a look at the MaxMind API (https://www.maxmind.com/en/geolite2-developer-package). …
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

770 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