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?)

1
  • 1
    what sql server are you using? MySQL, MSSQL, Postgres? Commented May 24, 2016 at 16:02

1 Answer 1

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;
Sign up to request clarification or add additional context in comments.

1 Comment

Oh, thanks for the suggestion. I'm using MSSQL but I'll look for something similar

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.