2

I have a table containing data in following format

id    |    name   |   age
----------------------------
1     |    John   |   24
2     |    Sam    |   NULL
3     |    Ron    |   32
4     |    Harry  |   44

now i want to fetch this into one row like

1:John:24,2:Sam,3:Ron:32,4:Harry:44

I have tried group_concat but it gives me only one columns values separated by comma, is it possible in mysql ?

2 Answers 2

4

Use group_concat and concat together:

SELECT group_concat(concat(id, ':', name, ':', IFNULL(age,''))) 
FROM table1

You can do this to get the ":" moved over

SELECT group_concat(concat(id, ':', name, IFNULL(concat(':',age),''))) 
FROM table1

And here's an updated SQLFiddle that hims056 created.

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

4 Comments

But, this drops Sam (null age). Need to take that into account also.
@prashant - He has already updated. See this SQLFiddle
@prashant You might need to move the : into the IFNULL if you don't need it when age is null.
@xecaps12 how can I move : into IFNULL can you please elaborate little more
0

Use this:

 select concat(id, ':',name, ':', age) as total_concated 
 from your_table 
 where 1

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.