How can I reduce multiple lines in just only one

I have a query that returns data in this structure: name | age | idMessage.

So, I have results like that:

John | 34 | 1
John | 34 | 2
John | 34 | 3

So the query returns multiple lines but I need to deal with it to show just one line in my frontend with all idMessages separated by comma.

How can I agregate this data in just one line but with all idMessage values like that:
John | 34 | 1, 2, 3

I tried use filter but it not seems the best way to deal with it.