write results of sql query to a file in mysql

2020-05-18 04:54发布

问题:

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.

回答1:

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


回答2:

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.



回答3:

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'


回答4:

If you are running mysql queries on the command line. Here I suppose you have the list of queries in a text file and you want the output in another text file. Then you can use this. [ test_2 is the database name ]

COMMAND 1

mysql -vv -u root -p test_2  < query.txt >  /root/results.txt 2>&1

Where -vv is for the verbose output.

If you use the above statement as

COMMAND 2

mysql -vv -u root -p test_2  < query.txt  2>&1 >  /root/results.txt

It will redirect STDERR to normal location (i.e on the terminal) and STDOUT to the output file which in my case is results.txt

The first command executes the query.txt until is faces an error and stops there.

That's how the redirection works. You can try

#ls key.pem asdf > /tmp/output_1 2>&1 /tmp/output_2

Here key.pm file exists and asdf doesn't exists. So when you cat the files you get the following

# cat /tmp/output_1
key.pem
#cat /tmp/output_2
ls: cannot access asdf: No such file or directory

But if you modify the previous statement with this

ls key.pem asdf > /tmp/output_1 > /tmp/output_2 2>&1

Then you get the both error and output in output_2

cat /tmp/output_2

ls: cannot access asdf: No such file or directory
key.pem


标签: mysql file-io