1

I tried for hours and read many posts but I still can't figure out how to handle this request

I have a table like this

Name        Phone

John        62778282
Alok        67828888
Rahul       68999999
Yash        97788899
John        99989999
Rahul       99992222

The output should be something like:

Name        Phone

John        62778282 , 99989999
Alok        67828888
Rahul       68999999 , 99992222
Yash        97788899

Please Anybody help me to get this result

Slava Rozhnev
  • 9,510
  • 6
  • 23
  • 39
  • Check out [this](https://stackoverflow.com/questions/13451605/how-to-use-group-concat-in-a-concat-in-mysql) question. It has a bit more complicated case than yours, but it's using what you need. You need to group concat the phone numbers and group by name. – El_Vanja Dec 06 '20 at 15:40
  • Does this answer your question? [How to use GROUP\_CONCAT in a CONCAT in MySQL](https://stackoverflow.com/questions/13451605/how-to-use-group-concat-in-a-concat-in-mysql) – nbk Dec 06 '20 at 15:41
  • You mention PHP, so consider handling display issues there instead – Strawberry Dec 06 '20 at 16:27

2 Answers2

2

In this case you could use GROUP_CONCAT with GROUP BY:

SELECT 
    `Name`,
    GROUP_CONCAT(`Phone`) AS `Phone`
FROM `Table`
GROUP BY `Name`;

Result SQL query

+=======+===================+
| Name  | Phone             |
+=======+===================+
| Alok  | 67828888          |
| John  | 62778282,99989999 |
| Rahul | 62778282,99992222 |
| Yash  | 97788899          |
+-------+-------------------+
Strawberry
  • 33,750
  • 13
  • 40
  • 57
Slava Rozhnev
  • 9,510
  • 6
  • 23
  • 39
2

Group By And GROUP_CONCAT

SELECT Name, GROUP_CONCAT(Phone) AS Phone  FROM phonetable GROUP BY Name
nbk
  • 45,398
  • 8
  • 30
  • 47