2

(I'm quite sure this question is answered here somewhere, but I'm not finding it, nor being able to put together the terms for a good search. So I'm sorry in advance if this question is duplicate.)

Here's my problem:

If I have a SELECT statement like this:

SELECT a.Id, b.Id
FROM TableA as a
JOIN TableB as b on a.Id = b.Id

And I get a result like this:

+-----+------+
|a.Id | b.Id |
|  1  |  1   |
|  1  |  2   |
|  2  |  1   |
|  2  |  3   |
|  2  |  4   |
|  3  |  3   |
+-----+------+

I would like to know how can I approach the problem of having to get this:

+-----+----------+
|a.Id | b.Id     |
|  1  |  1, 2    |
|  2  |  1, 3, 4 |
|  3  |  3       |
+-----+----------+

Further info / Edit: I'm using MSSQL. And yes, this question is duplicated: (Does T-SQL have an aggregate function to concatenate strings?)

Community
  • 1
  • 1
Mari Faleiros
  • 898
  • 9
  • 24

1 Answers1

0

In mysql you can use group_concat

SELECT a.Id, group_concat(b.Id)
FROM TableA as a
JOIN TableB as b on a.Id = b.Id
group by a.id;
ScaisEdge
  • 131,976
  • 10
  • 91
  • 107