The following is an example of how to run a query on the command line to output the result of a MySQL query to a CSV file.
Create a text file with the your query, query.sql:
SELECT * FROM hosts;
The run the following command:
mysql –skip-column-names -uuser -ppassword database < query.sql | sed ‘s/\t/”,”/g;s/^/”/;s/$/”/;’ > filename.csv
This will run the MySQL query and output the results to a text file in .csv format.
The sed commands do the following:→ Continue reading “Retuning a MySQL Query in CSV”