0

I have a table in mysql with n rows and columns

col1 col2
a     b
c     d

...

Using mysql procedures, how would I get all the rows into 1 varchar variable?

DECLARE var varchar(4096);
SET var = <select * from table>

The value of var should be "a b\nc d\n...."

Thank you.

1 Answer 1

3

You could do:

select var := group_concat(col1, ' ', col2 separator '
')
from table;

Seems a bit strange, but it will do what you want.

EDIT:

You can also do:

select var := group_concat(col1, ' ', col2 separator '\n')
from table;
Sign up to request clarification or add additional context in comments.

2 Comments

Can you please explain. What is 'separator'? Are you sure about the syntax? There are 2 quotes after separator - one of them is on a newline.
@user674669 . . . Usually \n means a new line. That is an easy way to put one in, without worrying about escaping things. separator is a keyword for group_concat().

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.