write results of sql query to a file in mysql

You could try executing the query from the your local cli and redirect the output to a local file destination;

mysql -user -pass -e"select cols from table where cols not null" > /tmp/output

This is dependent on the SQL client you're using to interact with the database. For example, you could use the mysql command line interface in conjunction with the "tee" operator to output to a local file:

http://dev.mysql.com/doc/refman/5.1/en/mysql-commands.html

tee [file_name], \T [file_name] 

Execute the command above before executing the SQL and the result of the query will be output to the file.

Specifically for MySQL Workbench, here's an article on Execute Query to Text Output. Although I don't see any documentation, there are indications that there should be also be an "Export" option under Query, though that is almost certainly version dependent.


You could try this, if you want to write MySQL query result in a file.

This example write the MySQL query result into a csv file with comma separated format

SELECT id,name,email FROM customers
INTO OUTFILE '/tmp/customers.csv'
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
LINES TERMINATED BY '\n'