How do I store a MySQL query result into a local CSV file?

Use the concat function of mysql

mysql -u <username> -p -h <hostname> -e "select concat(userid,',',surname,',',firstname,',',midname) as user from dbname.tablename;" > user.csv

You can delete the first line which contains the column name "user".


You're going to have to use the command line execution '-e' flag.

$> /usr/local/mysql/bin/mysql -u user -p -h remote.example.com -e "select t1.a,t1.b from db_schema.table1 t1 limit 10;" > hello.txt

Will generate a local hello.txt file in your current working directory with the output from the query.

Tags:

Mysql