Reputation: 516
I am joining different tables, and I wanted it to return a concatinated value. As a basic example, let me use the table .people
and .contacts
. What I wanted to happen is to query directyly(if possible) to return the all result of .people
with the corresponding values from .contacts
To give you a detail, this is the structure of .people
+----+--------+
| id | name |
+----+--------+
| 1 | naaa |
| 2 | hey |
+----+--------+
and this is the .contacts
+----+--------+--------+
| id | per_id | num |
+----+--------+--------+
| 1 | 1 | 1234 |
| 2 | 1 | 3456 |
| 3 | 1 | 8901 |
| 4 | 2 | 1111 |
+----+--------+--------+
I want it to somehow return
+----+--------+--------------------+
| id | name | num |
+----+--------+--------------------+
| 1 | naaa | 1234,3456,8901 |
| 2 | hey | 1111 |
+----+--------+--------------------+
is that doable with MySQL Query?
Upvotes: 0
Views: 811
Reputation: 3106
Check This.
SELECT p.id, p.name, GROUP_CONCAT(num SEPARATOR ', ') as num
FROM people as p
INNER JOIN contacts as c on p.id = c.per_id
GROUP BY p.id
Upvotes: 0
Reputation: 2762
You can achieve it using `GROUP_CONCAT':
SELECT p.id, p.name, GROUP_CONCAT(num SEPARATOR ', ')
FROM people as p
INNER JOIN contacts as c on p.id = c.per_id
GROUP BY p.id
Upvotes: 1