Mysql output in csv without using outfile in request

In the next command, how can I output data that will be separated by a comma (i.e. csv), I need the output in csv, but I do not want to use it into outfile. Any way to do this by running a query,

  mysql -uroot -ppassword -h112.30.16.11 -e 'select * from Employee.conditions' > /home/tom/preweb/static/users/aa.com/output.csv
+4
source share
1 answer

Try this, note that each column name and separators must be added separately.

mysql -uroot -ppassword -h112.30.16.11 -e 'SELECT CONCAT(col1, ',', col2, ',', col3) FROM agents;' > /home/tom/preweb/static/users/aa.com/output.csv

Comment based update:

This can be done dynamically by using prepared statements, in which case the table name should be added in the first parameter, where 'table-name'

mysql -uroot -ppassword -h112.30.16.11 -e "SET @VTable = 'table-name'; SET @VAllCols = CONCAT('SELECT CONCAT(',(SELECT GROUP_CONCAT(COLUMN_NAME SEPARATOR ',\',\',') FROM information_schema.columns WHERE TABLE_NAME = @VTable GROUP BY table_name),') FROM ', @VTable, ';'); PREPARE stmt FROM @VAllCols; EXECUTE stmt; DEALLOCATE PREPARE stmt;" > /home/tom/preweb/static/users/aa.com/output.csv

Let me know if you need anything else, like column headers.

Hi,

James

+2
source

Source: https://habr.com/ru/post/1652833/


All Articles