Reputation: 167
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.
Upvotes: 1
Views: 42
Reputation: 521289
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:
Demo here:
Upvotes: 1