Search code examples
mysqlsqlperlcommanddelimiter

Mysql : Get comma delimited Output


I am connecting to MySQL server and executing a select statement using Perl backticks. The output of the command is being captured in an array as shown below:

my @output = `mysql -u <user> -p<password> -e 'select * from <database_name>.<table_name>' -s`;

The -e option gives me tab delimited output with each row on a new line (batch mode) and -s gives minimal output in a non tabular format(silent mode).

Is there an option in the MySQL command to get a coma delimited output instead of tab delimited ?

(NOTE: I want to avoid concatenating values in the sql query)


Solution

  • There is no obvious option to do this (the options are here). You can change the query to get what you want:

    select concat_ws(',', col1, col2, . . . )
    from <database_name>.<table_name>
    

    But this requires listing all the columns (which I personally think is a good thing). You can also do the substitution after the fact.