1

I have a situation where I need to group SQL results by a specific field, while at the same time I need all values of a specific column of the grouped rows to be returned in one field (like so values1,value2,value3 etc)

example data:

NAME id 
john 1
john 2
john 3
maria 4
maria 5

result I need:

john 1,2,3
maria 4,5

Is this possible and how?

PM 77-1
  • 12,933
  • 21
  • 68
  • 111
sakattack
  • 53
  • 1
  • 7

1 Answers1

0

This is possible and here is the simple example of GROUP_CONCAT, You may get your desired output after run this/ execute this. The example is with a delimiter ', ' as well, so you can quick understand the things.

The GROUP_CONCAT function returns a string result with the concatenated non-NULL values from a group. It returns NULL if there are no non-NULL values

SELECT name,
    GROUP_CONCAT(DISTINCT id ORDER BY id DESC SEPARATOR ', ')
    FROM table_name
GROUP BY name;

This may help you.

Murad Hasan
  • 9,565
  • 2
  • 21
  • 42