user2037445 user2037445 - 7 months ago 9
SQL Question

Concatenating columns with mysql

I am struggling with a small problem. I have a table as follows

name id
a 1
b 1
c 2
d 2
e 3
f 4


I need output as follows.

a-b 1
c-d 2
e 3
f 4


I need to get the concatenated name for a given
ID
.I tried using concat function but i ended up empty handed.

Answer
SELECT id
     , GROUP_CONCAT(name SEPARATOR '-') AS name 
FROM table 
GROUP BY id