简体   繁体   中英

MySQL - Select Concat an entire row

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?

You will have the build the query dynamically, ie 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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM