Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

write results of sql query to a file in mysql

Tags:

file-io

mysql

I'm trying to write the results of a query to a file using mysql. I've seen some information on the outfile construct in a few places but it seems that this only writes the file to the machine that MySQL is running on (in this case a remote machine, i.e. the database is not on my local machine).

Alternatively, I've also tried to run the query and grab (copy/paste) the results from the mysql workbench results window. This worked for some of the smaller datasets, but the largest of the datasets seems to be too big and causing an out of memory exception/bug/crash.

Any help on this matter would be greatly appreciated.

like image 414
Ramy Avatar asked Jun 13 '11 16:06

Ramy


People also ask

How can we save the result of SQL query in a MySQL file?

Save MySQL Results to a File We simply add the words INTO OUTFILE, followed by a filename, to the end of the SELECT statement. For example: SELECT id, first_name, last_name FROM customer INTO OUTFILE '/temp/myoutput.

How do you save MySQL query result in a Excel file?

If you require to save the results of your MYSQL query to a CSV or Excel sheet, you can do so with the help of 'INTO OUTFILE'. This saves the query result as a 'CSV'. You can open this CSV file in Excel and save it as a . XLS file as well.


1 Answers

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 
like image 99
eroomydna Avatar answered Sep 20 '22 14:09

eroomydna