Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

MySQL merge data into one column

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

| 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.

>Solution :

For the second format you could use GROUP_COUNCAT():

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

Fiddle

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading