11

How can I concatenate all the rows in single rows when I fire SELECT query?

enter image description here

I want O/P like

101 abc CA USA 102 xyz PH UK 103 pqr WDC EU

Any help kindly appreciated. Thanks

1
  • yipes... why? this will not scale at all. Commented Feb 2, 2012 at 17:03

2 Answers 2

12

Use conbination of group_concat and concat functions

 SELECT group_concat( concat( id, " ",name," ",city," ",state," " ) SEPARATOR ' ')
 FROM tablename
Sign up to request clarification or add additional context in comments.

Comments

12

You'll need GROUP_CONCAT and CONCAT mysql functions and the query should look like this:

SELECT GROUP_CONCAT( CONCAT( id, ' ', name, ' ', city, ' ', state) SEPARATOR ' ')
FROM students
GROUP BY (1)

Or you can use CONCAT_WS instead:

CONCAT_WS(' ', id, name, city, state)

1 Comment

Unfortunately the links to GROUP_CONCAT and CONCAT_WS are broken :-/

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.