0

There are few rows:

Id marks 
1  15   
1  16
1  17  
2  6
2  15
3  9
3  10

I want to merge all the id into a single row, with output like below:

 Id  marks1 marks2 marks3 
 1   15    16      17
 2   6     15
 3   9     10
0

1 Answer 1

1

Concatenating fields into a single field is made really easy in mysql. there is function for it. CONCAT(). Better yet if you and want to delimit them by the same string you can use CONCAT_WS(). which is concatenate with separator, the first parameter being the separator and any other argument after being a field you wish to join.

http://www.w3resource.com/mysql/string-functions/mysql-concat_ws-function.php

SELECT id, CONCAT_WS(' ', marks1, marks2, marks3) as 'mark1 | mark2 | mark3' from my_table group by id;

UPDATE

I think I Misunderstood your schema structure. I think what you actually are looking for is GROUP_CONCAT().

http://www.w3resource.com/mysql/aggregate-functions-and-grouping/aggregate-functions-and-grouping-group_concat.php

SELECT id, GROUP_CONCAT(marks) as 'All Marks' from my_table group by id;
Sign up to request clarification or add additional context in comments.

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.