1

I need to concat all columns in a string of a row. Something like this can be possible?

SELECT CONCAT_WS(';', *) FROM db.table

Table:

|col1   |col2   |col3   |
-------------------------
|val11  |val12  |val13  |
|val21  |val22  |val23  |

Result:

val11;val12;val13
val21;val22;val23

Thanks.

Riccardo
  • 33
  • 4
  • 2
    You need to specify all the columns by name in the concat statement or use dynamic sql (building a sql statement from information_schema.columns) if you don't want to do that. https://dev.mysql.com/doc/refman/8.0/en/sql-prepared-statements.html – P.Salmon Sep 30 '20 at 12:04
  • 2
    If you have so many columns that you don't want to write them down then your table design is most probably wrong. – juergen d Sep 30 '20 at 12:08
  • @juergend the design of table are realy bad, but that not a my table and are so old – Riccardo Sep 30 '20 at 12:12
  • @P.Salmon okay thanks, so i need to crate a "dynamic" query – Riccardo Sep 30 '20 at 12:12
  • you can make a csv https://dev.mysql.com/doc/refman/5.7/en/select-into.html and load that – nbk Sep 30 '20 at 12:18
  • You probably need to fix your schema. – Strawberry Sep 30 '20 at 12:35
  • @nbk too long and useless operation – Riccardo Sep 30 '20 at 13:27
  • @Strawberry i can't it's a standard schema for other applications – Riccardo Sep 30 '20 at 13:27
  • 1
    @Riccardo then use a stored procedure, use https://stackoverflow.com/a/4165253/5193536 to get all column names and build a sql SELECt with CONCAT and use that with a prepared statement like P.Salmon said – nbk Sep 30 '20 at 13:52

0 Answers0