2

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.

2 Answers 2

8
SELECT id
     , GROUP_CONCAT(name SEPARATOR '-') AS name 
FROM table 
GROUP BY id
Sign up to request clarification or add additional context in comments.

3 Comments

@AngeloNeuschitzer Yeah,GROUP_CONCAT is one of Mysql`s gems.
Does it support for sql as well?
@user2037445 No,you have to do some acrobatics for that. social.msdn.microsoft.com/Forums/sqlserver/en-US/…
3
SELECT group_concat(`name` SEPARATOR '-') AS NAMES
     , `id` 
FROM Table1
GROUP BY `id`;

SQLFiddle

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.