?
Solved

MySQL Query

Posted on 2011-02-28
2
Medium Priority
?
310 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
2 Comments
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 2000 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

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

Question has a verified solution.

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

When table data gets too large to manage or queries take too long to execute the solution is often to buy bigger hardware or assign more CPUs and memory resources to the machine to solve the problem. However, the best, cheapest and most effective so…
Containers like Docker and Rocket are getting more popular every day. In my conversations with customers, they consistently ask what containers are and how they can use them in their environment. If you’re as curious as most people, read on. . .
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
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…
Suggested Courses
Course of the Month15 days, 1 hour left to enroll

839 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