1

My table.

id  Exam Name    isCombine
1   Reading         0
2   Writing         0
3   Maths3          1
4   Maths4          1

Above table is just an example. In real table there are much more columns. I want to combine row which has isCombine value 1 and leave other rows as it is.

I am expecting following output.

id      Exam Name           isCombine 
1       Reading                 0
2       Writing                 0
3,4     Maths3,Maths4           1,1

I am not sure, whether it is possible or not. However any help or suggestion would be much appreciated.

1
  • 1
    What do you mean by combine? Do you want a query that gets you all rows where 'isCombine' is '1'? Or what exactly is the purpose? Commented Sep 5, 2017 at 6:14

1 Answer 1

1

You could use a GROUP_CONCAT:

SELECT CAST(id AS CHAR(50)) AS id, `Exam Name`, CAST(isCombine AS CHAR(50)) AS isCombine
FROM yourTable
WHERE isCombine = 0
UNION ALL
SELECT
    GROUP_CONCAT(id),
    GROUP_CONCAT(`Exam Name`),
    GROUP_CONCAT(isCombine)
FROM yourTable
WHERE isCombine = 1

I did not include any specific ordering, nor did you specify any. If you wanted the combined rows to always appear e.g. on the bottom, we could slightly modify the above query for this requirement.

Output:

enter image description here

Demo here:

Rextester

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much.

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.