Home > Enterprise >  MySQL merge data into one column
MySQL merge data into one column

Time:01-09

I have this query result:

|  Activity    |  Player   |
| -----------  | --------- |
| Play Tennis  | John Doe  |
| Play Tennis  | Jane Doe  |
| Play Bowling | Jonah Doe |

But the goal is supposed to be like this:

| Activity     | Player              |
| -----------  | ------------------- |
| Play Tennis  | John Doe            |
|              | Jane Doe            |
| Play Bowling | Jonah Doe           |

or like this:

| Activity     | Player              |
| -----------  | ------------------- |
| Play Tennis  | John Doe, Jane Doe  |
| Play Bowling | Jonah Doe           |

How do I achieve that query?
I would like to share my current query and the full columns here but I couldn't due to a strict rules on my workplace (I hope everyone could understand it)

Thank you.

CodePudding user response:

For the second format you could use GROUP_COUNCAT():

SELECT Activity, GROUP_CONCAT(Player) AS Player
FROM table_name
GROUP BY Activity

Fiddle

  •  Tags:  
  • Related