Solved

MySQL Query

Posted on 2011-02-28
2
299 Views
Last Modified: 2012-06-22
I have two tables; a and b. The following is the structure for said tables:

a
---------
a_id int
name

b
---------
b_id int
a_id int
b_name

The two tables have a 1 to many relationship; therefore for every one record in table a, there could be many records in table b that are related.

Here is the problem....

I am running the following SQL and getting the following results:

SELECT a.a_id, b.b_id FROM a INNER JOIN b ON a.a_id = b.a_id;

1,1
1,2
1,3
1,4
1,5
1,6
2,1
2,6
3,1
3,4
4,1
5,2

Instead of getting a multiple returned rows for each record in table b, is there a way to get them on a single line so the returned record sets look like this:

1,1,2,3,4,5,6
2,1,6
3,1,4
4,1
5,2
0
Comment
Question by:plecostomus
[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 Comments
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
ID: 35001147
please try:
SELECT a.a_id, group_concat(b.b_id)
 FROM a INNER JOIN b ON a.a_id = b.a_id
 GROUP BY a.a_id;

Open in new window

0
 

Author Closing Comment

by:plecostomus
ID: 35001826
Worked perfectly
0

Featured Post

Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

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

More Fun with XML and MySQL – Parsing Delimited String with a Single SQL Statement Are you ready for another of my SQL tidbits?  Hopefully so, as in this adventure, I will be covering a topic that comes up a lot which is parsing a comma (or other…
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…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

739 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