Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL how do you append 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 are to continually keep on appending to Results.csv

like image 220
stackoverflow Avatar asked Mar 15 '12 13:03

stackoverflow


People also ask

How do I use Outfile in MySQL?

You can also use SELECT ... INTO OUTFILE with a VALUES statement to write values directly into a file. An example is shown here: SELECT * FROM (VALUES ROW(1,2,3),ROW(4,5,6),ROW(7,8,9)) AS t INTO OUTFILE '/tmp/select-values.

What is append mode in SQL?

An append query selects records from one or more data sources and copies the selected records to an existing table. For example, suppose that you acquire a database that contains a table of potential new customers, and that you already have a table in your existing database that stores that kind of data.

How do I insert selected columns from a CSV file to a MySQL database using load data infile?

The code is like this: LOAD DATA INFILE '/path/filename. csv' INTO TABLE table_name FIELDS TERMINATED BY ',' LINES TERMINATED BY '\r\n' (column_name3, column_name5); Here you go with adding data to only two columns(you can choose them with the name of the column) to the table.

How do you load a file into a MySQL table?

mysql> LOAD DATA LOCAL INFILE '/path/pet. txt' INTO TABLE pet; If you created the file on Windows with an editor that uses \r\n as a line terminator, you should use this statement instead: mysql> LOAD DATA LOCAL INFILE '/path/pet.


1 Answers

You can merge results and write at once. TEE will log everything which might not be desirable. In your case:

SELECT * FROM Results UNION 
SELECT * FROM Results INTO OUTFILE '~/TestInput/Results.csv';
like image 164
Gokulnath Avatar answered Sep 29 '22 01:09

Gokulnath