MySQL, how do you add to a file with INTO OUTFILE?

I have the following code:

SELECT * INTO OUTFILE'~/TestInput/Results.csv'      
FIELDS TERMINATED BY ','
LINES TERMINATED BY '\n'
FROM Results;

Desired results should be constantly added to Results.csv

+5
source share
3 answers

You cannot do this with SELECT INTO OUTFILE. This command only creates new files and will not work if the file already exists.

You can add query output to existing files using the command TEEin the MySQL client.

Here, your example adds two query results to a single file with TEE:

TEE ~/TestInput/Results.csv

SELECT * 
FROM Results;

SELECT * 
FROM Results;

NOTEE
+5
source

You can combine the results and write immediately. TEE registers everything that may be undesirable. In your case:

SELECT * FROM Results UNION 
SELECT * FROM Results INTO OUTFILE '~/TestInput/Results.csv';
+4
source

This cannot be done directly in MySQL. But you can try adding date-time to the file name and then merging some files into a new one using the "cat" (UNIX) or "type" command (DOS command).

Help: cat (Unix)

+1
source

All Articles