views:

437

answers:

2

How can I select and concat every field in a row?

I want to do something similar to this:

SELECT concat(SELECT GROUP_CONCAT(COLUMN_NAME)
FROM INFORMATION_SCHEMA.COLUMNS
WHERE table_name = 'tbl_name')
as single FROM tbl_name

..but obviously the above doesn't work. Any suggestions?

A: 

You will have the build the query dynamically, i.e. list all columns in concat(...) explicitly. You can do that on the client side by running two queries or in a stored procedure using a prepared statement.

Lukáš Lalinský
A: 

You can do it in 3 steps:

  1. Build field list

    SELECT group_concat( column_name ) as field_list FROM INFORMATION_SCHEMA.COLUMNS WHERE table_name = 'TABLE_NAME' GROUP BY table_name LIMIT 0 , 1

  2. Copy the value of field_list field

  3. Extract data:

    SELECT CONCAT( "Paste value copied at previous step" ) FROM TABLE_NAME

If you create a stored procedure, than you can combine the above steps

Adrian